Jquery 如何将onClick事件添加到带有类的标记中
这是我在一页上看到的Jquery 如何将onClick事件添加到带有类的标记中,jquery,append,Jquery,Append,这是我在一页上看到的 <a class="download-link" href="http://www.test.com/pages-">Pages 1</a> 下面是我想用jQuery实现的结果 <a class="download-link" href="http://www.test.com/pages-1.asp" onclick="_sz.push(['event', 'PDF', 'download', ('report') ])">Pag
<a class="download-link" href="http://www.test.com/pages-">Pages 1</a>
下面是我想用jQuery实现的结果
<a class="download-link" href="http://www.test.com/pages-1.asp" onclick="_sz.push(['event', 'PDF', 'download', ('report') ])">Pages 1</a>
jQuery
<script type="text/javascript">/
$('.download-link').each(function(){
this.href += '-1.asp';
this.attr('onclick', '_sz.push(['event', 'PDF', 'download-request', ('report') ])');
})
</script>
/
$('.download link')。每个(函数(){
this.href+='-1.asp';
this.attr('onclick','u sz.push(['event','PDF','download request',('report')]);
})
您可以在jquery中尝试prop
而不是attr
//before
this.attr('onclick', '_sz.push(...)');
//after
this.prop('onclick', '_sz.push(...)');
试试这个
$('.download-link').bind('click', function (event) {
_sz.push(['event', 'PDF', 'download', ('report')])
});
我希望如此。它可以帮助你。为什么你需要
onclick
?,只需使用$('.download link')。单击(function(){u sz.push(['event',PDF',download request',('nhs-companys-an-enterprising-approach-to-health'));}
在jQuery选择器中,你应该使用正确的类:$('download link)
@Satpal出于跟踪事件的分析原因,我需要它。使用JS添加一个调用JS的属性有点多余。按照@Satpal的建议,直接向元素添加一个不引人注目的事件处理程序