jquery – this.attr()不是一个函数?

我不太确定如果我没有在正确的范围内使用这个或什么,但我有一个脚本,基本上捕获链接点击,并导致页面淡出,然后再去链接页面.但是,如果链接javascript onclick,则脚本失败.这是我的代码
<script type="text/javascript">
pageObj = {
    init: function(){
        $("body").fadeto("slow",1);
    },redirectPage: function(redirect){
        window.location = redirect;
    },linkLoad: function(location){
        $("body").fadeOut(1000,this.redirectPage(location));
    }
};
$(document).ready(function() {
    pageObj.init();

    $("a").click(function(e){
        e.preventDefault();
        if (this.attr('onclick') !== undefined) {
            eval(this.attr('onclick').val());
        } else {
            var location = this.href;
            pageObj.linkLoad(location);
        }
    });
});
</script>

如您所见,我正在尝试检查链接是否具有onclick属性,然后调用onclick函数(如果存在).我该如何实现?

解决方法

虽然diodeus是正确的,您需要在使用attr()(它是一个jQuery集合的方法,而不是HTMLElement)之前将其包装在jQuery集合中,您可以跳过attr().
$("a").click(function(e){
    var location;
    e.preventDefault();
    if ($.isFunction(this.onclick)) {
        this.onclick.call(this,e);
    } else {
        location = this.href;
        pageObj.linkLoad(location);
    }
});

请注意,我使用该属性(当HTML文档加载时,属性通常预加载属性中,其中on_______属性预加载方法.另请注意,我使用this.onclick.call()而不是eval(),设置正确的对于onclick方法,并确保访问事件对象作为参数.

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...