![](/img/trans.png)
[英]Why does the onclick function run on onclick, but not when I call it manually?
[英]Why does the function run once when I put this.onclick=' ';?
<a href="#" onclick="addP(); this.onclick='';">See more</a>
为什么我把 this.onclick=' '; 放在 function 运行一次?
当您单击链接时。
addP
。现在实际的 HTML 看起来像
<a href="#" onclick="addP();">See more</a>
要解决此问题,请删除 onclick 属性中的onclick=''
部分。
let addP = () => alert('you clicked the link')
<a href="#" onclick="addP();">See more</a>
我认为它覆盖了本机方法:)
您必须删除this.onclick='';
因为当 o'clock 事件 ( addP
) 结束时,它会删除 click 事件处理程序。 因此,有两种方法适合您:
方法一
像这样使用 javascript 和 html :
HTML:
<a href="#" id="seeMore">See more</a>
Javascript:
document.getElementById('seeMore').addEventListener('click', function() {
addP();
});
方法二
仅使用 html
<a href="#" onclick="addP();">See more</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.