[英]Why does the function run once when I put this.onclick=' ';?
<a href="#" onclick="addP(); this.onclick='';">See more</a>
Why does the function run once when I put this.onclick=' ';?为什么我把 this.onclick=' '; 放在 function 运行一次?
When you clicked the link.当您单击链接时。
addP
.addP
。 Now the actual HTML looks like现在实际的 HTML 看起来像
<a href="#" onclick="addP();">See more</a>
To fix this remove onclick=''
part in the onclick attribute.要解决此问题,请删除 onclick 属性中的
onclick=''
部分。
let addP = () => alert('you clicked the link')
<a href="#" onclick="addP();">See more</a>
I think it's overriding the native method:)我认为它覆盖了本机方法:)
You have to remove this.onclick='';
您必须删除
this.onclick='';
because when the o'clock event ( addP
) Is over, it removes the the click event handler.因为当 o'clock 事件 (
addP
) 结束时,它会删除 click 事件处理程序。 So there are 2 methods for you:因此,有两种方法适合您:
Method 1方法一
use javascript & html like so:像这样使用 javascript 和 html :
HTML: HTML:
<a href="#" id="seeMore">See more</a>
Javascript: Javascript:
document.getElementById('seeMore').addEventListener('click', function() {
addP();
});
Method 2方法二
only using html仅使用 html
<a href="#" onclick="addP();">See more</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.