简体   繁体   English

捕获JavaScript onClick JQuery

[英]capture JavaScript onClick JQuery

I have a div that pops up when a user clicks on a link that will take them off of the english version of the site. 当用户单击将其从该网站的英语版本中删除的链接时,我会弹出一个div。 They are given a yes or no choice to proceed. 他们被给予是或否的选择。 I have to capture the "URL" of where they will go to if they click yes. 如果他们单击“是”,我必须捕获他们将转到的“ URL”。 This is easy on the links that are a hrefs, but some of the links are onClicks that execute JavaScript. 在作为href的链接上这很容易,但是某些链接是执行JavaScript的onClicks。 I have to capture the JavaScript, keep it from executing, and then execute it if they click yes. 我必须捕获JavaScript,阻止其执行,如果单击“是”,则执行它。 This is the capture onClick code I've got, but how do I get it to work? 这是我获得的捕获onClick代码,但是如何使它工作呢?

$_('a#foo').live('click', function(event) {
    var onclick = $(this).attr("onclick");
    $_('#yes a').attr('onclick', onclick);*/
    return false;
    event.preventDefault();
});

the yes button/div 是按钮/ div

$_('#yes').live('click', function() {
    if(href) {
        window.location=href;
    };
    if(onclick) {
        eval(onclick);
    }
});

Here's a working demo 这是一个工作演示

Basically, if an onclick attribute is defined in the html, it'll be accessible as somelink.onclick which is just a function, so there's no need for eval 基本上,如果在html中定义了onclick属性,则可以将其作为somelink.onclick进行访问,它只是一个函数,因此不需要eval

The trick is however, that code defined in an onclick-attribute will execute before the jQuery live() event handler, so you have to avoid that by removing the original onclick function but keeping a copy you can execute later. 但是,诀窍在于,在onclick-attribute中定义的代码将在jQuery live()事件处理程序之前执行,因此您必须通过删除原始的onclick函数来避免这种情况,但保留一个副本即可稍后执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM