Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何将onClick事件添加到带有类的标记中_Jquery_Append - Fatal编程技术网

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的建议,直接向元素添加一个不引人注目的事件处理程序