jQuery:this.attr()不是函数?

时间:2022-12-05 16:11:19

I'm not quite sure if I'm not using this in the correct scope or what, but I have a script that basically captures a link click and causes the page to fade out before going to the linked page. However, if the link is a JavaScript onclick, the script fails.

我不太确定我是否在正确的范围内使用它或者什么,但是我有一个脚本基本上捕获链接点击并导致页面在转到链接页面之前淡出。但是,如果链接是JavaScript onclick,则脚本将失败。

Here's my code:

这是我的代码:

<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>


As you can see, I'm trying to do a check to see if the link has the onclick attribute, and then call the onclick function if it exists. How can I achieve this?

正如您所看到的,我正在尝试检查链接是否具有onclick属性,然后调用onclick函数(如果存在)。我怎样才能做到这一点?

2 个解决方案

#1


4  

While Diodeus is correct that you need to wrap this in a jQuery collection before using attr() (it's a method of a jQuery collection, not of an HTMLElement), you can just as well skip attr().

虽然Diodeus是正确的,你需要在使用attr()之前将它包装在jQuery集合中(它是一个jQuery集合的方法,而不是HTMLElement的方法),你也可以跳过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);
    }
});

Note that I used the property (when an HTML document loads, attributes are usually preloaded into properties, with on_______ attributes being preloaded as methods. Also note that I used this.onclick.call() rather than eval(), setting the correct this for onclick methods, and ensuring access to the event object as an argument.

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

#2


67  

Use: $(this).attr instead of this.attr

使用:$(this).attr而不是this.attr

This forces it into the context of jQuery.

这迫使它进入jQuery的上下文。

#1


4  

While Diodeus is correct that you need to wrap this in a jQuery collection before using attr() (it's a method of a jQuery collection, not of an HTMLElement), you can just as well skip attr().

虽然Diodeus是正确的,你需要在使用attr()之前将它包装在jQuery集合中(它是一个jQuery集合的方法,而不是HTMLElement的方法),你也可以跳过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);
    }
});

Note that I used the property (when an HTML document loads, attributes are usually preloaded into properties, with on_______ attributes being preloaded as methods. Also note that I used this.onclick.call() rather than eval(), setting the correct this for onclick methods, and ensuring access to the event object as an argument.

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

#2


67  

Use: $(this).attr instead of this.attr

使用:$(this).attr而不是this.attr

This forces it into the context of jQuery.

这迫使它进入jQuery的上下文。