jQuery的 - this.attr()不是一个函数? [英] jquery - this.attr() not a function?

查看:233
本文介绍了jQuery的 - this.attr()不是一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太清楚,如果我不是在正确的范围或使用什么,但我有一个脚本,基本上抓住了一个链接点击导致页面才去链接页面淡出。但是,如果该链接是一个JavaScript的onclick,该脚本将失败。这里是我的code:

 <脚本类型=文/ JavaScript的>
pageObj = {
    初始化:功能(){
        $(机构)fadeTo(慢,1);
    },
    redirectPage:功能(重定向){
        window.location的重定向=;
    },
    linkLoad:功能(位置){
        $(机构)淡出(1000,this.redirectPage(位置))。
    }
};
$(文件)。就绪(函数(){
    pageObj.init();    $(A)。点击(函数(五){
        亦即preventDefault();
        如果(this.attr('的onclick')!==未定义){
            的eval(this.attr('的onclick')VAL());
        }其他{
            VAR位置= this.href;
            pageObj.linkLoad(位置);
        }
    });
});
< / SCRIPT>

正如你所看到的,我试图做一个检查,看看是否有链接onclick属性,然后调用的onclick功能,如果它的存在。我怎样才能做到这一点?


解决方案

虽然Diodeus是正确的,你需要用这个在一个jQuery集合使用<$ C $前C> ATTR()(这是一个jQuery集合的方法,而不是一个 HTML元素),你可以同时跳过 ATTR()

  $(A)。点击(函数(五){
    VAR位置;
    亦即preventDefault();
    如果($ .isFunction(this.onclick)){
        this.onclick.call(这一点,E);
    }其他{
        位置= this.href;
        pageObj.linkLoad(位置);
    }
});

请注意,我用的财产(当HTML文档载入,属性通常是ploaded到性能$ P $,与上_______ 属性ploaded的方法是$ P $。另外请注意,我用 this.onclick.call(),而不是的eval(),设置正确的这个的onclick 方法,并确保获得事件对象作为参数。<​​/ p>

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

解决方案

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().

$("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.

这篇关于jQuery的 - this.attr()不是一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆