你如何覆盖内联onclick事件? [英] How do you override inline onclick event?
本文介绍了你如何覆盖内联onclick事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何用JavaScript重写以下HTML中的onclick事件?
< a id =samplehref =...onclick =alert('hello world')/> ;...< / a>
我已经用jQuery试过了,但那并没有办法:
$ b $
$('#sample')。attr('onclick','alert(done)')
假设HTML是预先写好的,不能改变,除了用JavaScript来操作。
<试试这个:
//设置DOM元素的onclick为null删除
//内联点击处理程序
$(#sample)[0] .onclick = null;
$(#sample)。click(function(){alert(done)});
This seems deceptively simple.
How do you override the onclick event in the following HTML using JavaScript?
<a id="sample" href="..." onclick="alert('hello world')" />...</a>
I've tried it with jQuery, but that didn't do the trick:
$('#sample').attr('onclick','alert("done")')
Assume the HTML is pre-written and cannot be changed, other than manipulating it with JavaScript.
解决方案
Try this:
// Setting the DOM element's onclick to null removes
// the inline click handler
$("#sample")[0].onclick = null;
$("#sample").click(function() { alert("done") });
这篇关于你如何覆盖内联onclick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文