[英]dynamically added onclick event to anchor tag, not working
我的jsp中有以下锚标签
<a href="javascript:submit(this)">save</a>
<a href="javascript:alert(this)">something</a>
我需要停止默认的href行为,但必须在onclick上调用这些功能。 同样,我编写了这段代码:
$('a[href^=\'#\']').live("click", function (e) {
alert("I am being called #");
e.preventDefault();
return false;
});
$(document).ready(function(){
$("a[href^='javascript']").each(function(){
alert("replacing");
var w=$(this).attr('href');
$(this).attr('href','#');
$(this).attr("onclick",w);
});
});
但是,单击链接时不会调用这些功能。 你能告诉我我要去哪里错了。 我正在使用IE 8。
尝试这个:
$(document).on("click", "a[href^=\'#\']",function (e) {
alert("I am being called #");
e.preventDefault();
return false;
});
$(document).ready(function(){
$("a[href^='javascript']").attr('href','#');
});
$(document).ready(function() { var mydiv = document.getElementById("myDiv"); var aTag = document.createElement('a'); aTag.setAttribute('href', "#"); aTag.setAttribute('id', "tagId"); aTag.innerHTML = "link text"; mydiv.appendChild(aTag); $("#tagId").on('click', function(e) { alert(1) }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.