Javascript 使用 jquery 将 onClick 事件附加到锚标记?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3057727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append an onClick event to an anchor tag using jquery?
提问by Matthew Woodard
I am trying to add an onClick event to an anchor tag ...
我正在尝试向锚标记添加 onClick 事件...
Previously i had ...
以前我有...
<a href="somlink.html" onClick="pageTracker._link(this.href); return false;">
<a href="somlink.html" onClick="pageTracker._link(this.href); return false;">
But i am trying to avoid the inline onClick event because it interferes with another script..
但我试图避免内联 onClick 事件,因为它会干扰另一个脚本。
So using jQuery i am trying the following code ...
所以使用jQuery我正在尝试以下代码......
<script>
$(document).ready(function() {
$('a#tracked').attr('onClick').click(function() {window.onbeforeunload = null;
pageTracker._link(this.href);
return false;
});
});
</script>
with the html like so <a id="tracked" href="something.html">
像这样的html <a id="tracked" href="something.html">
So my question is should this be working, and if not what would be the best solution?
所以我的问题是这应该有效吗,如果不是,最好的解决方案是什么?
回答by realshadow
The correct way would be (as for jQuery)
正确的方法是(至于 jQuery)
$('#tracked').click(function() {
pageTracker._link($(this).attr('href'));
return false;
});
This will add an "onclick" event on any element with tracked id. There you can do anything you want. After the click event happens, the first line will pass href attribute of the clicked element to pageTracker.
这将在任何具有跟踪 ID 的元素上添加一个“onclick”事件。在那里你可以做任何你想做的事。单击事件发生后,第一行将被单击元素的 href 属性传递给 pageTracker。
As for your original question, it wouldnt work, it will raise undefined error. The attr works a bit different. See documentation . The way you used it, would return the value of the attribute and I think that in that case its not chainable. If you would like to keep it the way you had it, it should look like this:
至于你原来的问题,它不起作用,它会引发未定义的错误。attr 的工作方式有点不同。请参阅文档。您使用它的方式将返回属性的值,我认为在这种情况下它不可链接。如果您想保持原样,它应该如下所示:
$('#tracked').click(function() {
$(this).attr('onclick', 'pageTracker._link(this.href); return false;');
return false;
});
回答by user2240271
You can also try
你也可以试试
var element1= document.getElementById("elementId");
and then
进而
element1.setAttribute("onchange","functionNameAlreadyDefinedInYourScript()");
// here i am trying to set the onchange event of element1(a dropdown) to redirect to a function()
// 在这里,我试图将 element1(a dropdown) 的 onchange 事件设置为重定向到 function()
回答by TimSmith-Aardwolf
I spent some time on this yesterday. It turned out that I needed to include the jQuery on $(window).ready not $(document).ready.
我昨天花了一些时间在这上面。事实证明,我需要在 $(window).ready 上包含 jQuery,而不是 $(document).ready。
$( window ).ready(function() {
$('#containerDiv a').click(function() {
dataLayer.push({
'event': 'trackEvent',
'gtmCategory': 'importantLinkSimilarProperties',
'gtmAction': 'Click',
'gtmLabel': $(this).attr('href')
});
});
});

