gpt4 book ai didi

javascript - 使用 JavaScript 更改 onclick 事件不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 05:02:45 24 4
gpt4 key购买 nike

我有这个:

function tp_visible(action){
if(action==1){
document.getElementById("tp").style.display='block';
document.getElementById("tp_action").onclick='tp_visible(0);
return false;';
}
else {
document.getElementById("tp").style.display='none';
document.getElementById("tp_action").onclick='tp_visible(1);
return false;';
}
return false;
}

为什么上面没有改变 onclick 事件?

我使用 Firebug并且事件保持不变......

这是 HTML:

<a
id='tp_action'
name='tp_action'
href='#'
onclick='tp_visible(1);
return false;'
>Show info</a>

最佳答案

下面的检查对您有用,因为您不能将字符串值分配给处理程序 onclick

function tp_visible(action){
if(action==1){
document.getElementById("tp").style.display='block';
document.getElementById("tp_action").onclick= function ()
{ tp_visible(0); return false;};
}
else {
document.getElementById("tp").style.display='none';
document.getElementById("tp_action").onclick= function ()
{tp_visible(1); return false; }
}
return false;
}

关于javascript - 使用 JavaScript 更改 onclick 事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4244401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com