我想将属性onClick添加到使用jquery自动生成的链接中。

我选择父div,然后搜索它是否具有链接子级,然后添加属性onClick。

它可以在本地主机上运行,​​但不能在服务器上运行
有我的代码:

$('div.holder_notify_drop_data,li.holder_notify_drop_data').each(function () {
    if ($(this).children('a').hasClass('holder_notify_drop_link')) {
        $(this).on('click', function (e) {
            var url = $(this).children('a').attr('href');
            $(this).children('a').attr('onClick', "openFrame('" + url + "','yes')");
            e.preventDefault();
        });
    };)
};


我怎样才能做到这一点 ?

最佳答案

确保包含jQuery:

<script src="http://code.jquery.com/jquery-latest.js"></script>


重要提示:还要将代码放入可用于文档的函数中,我建议您使用jQuery click()函数代替已完成的工作,但这没关系:

$(document).ready(function () {

$('div.holder_notify_drop_data,li.holder_notify_drop_data').each(function(){
if ($(this).children('a').hasClass('holder_notify_drop_link')){
$(this).on('click',function(e) {
var url = $(this).children('a').attr('href');
$(this).children('a').attr('onClick',"openFrame('"+url+"','yes')");
  e.preventDefault();
});
};
)};

});

10-06 12:42
查看更多