我想将 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();
});
};
)};
});
关于javascript - 如何将 onClick 属性添加到自动生成的链接中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22480088/