这里的好心人最近帮助我使用了下面发布的一些 JavaScript。但是,它似乎与网站上已有的另一个功能冲突。由于我是后端程序员,对JavaScript了解甚少,请问有谁知道如何解决冲突吗?
function showhide(id) {
if (document.getElementById) {
var divid = document.getElementById(id);
var divs = document.getElementsByClassName('hide');
var showElement = true;
if (divid.style.display === 'block') {
showElement = false;
}
for (var i = 0; i < divs.length; i++) {
divs[i].style.display = 'none';
}
if (showElement) {
divid.style.display = 'block';
}
}
return false;
}
上面的代码与下面的代码冲突,下面是验证码的重新加载图标。没有错误,但只要其他代码存在,刷新就不会执行任何操作。请注意,上面的代码包含在整个站点范围内,并在其他几个页面上使用,而下面的代码仅在其中一个页面上使用。
<img src="/common/quickcaptcha/imagebuilder.php" border="1" id="verification">
<a href="#" onclick="document.getElementById('verification').src='/common/quickcaptcha/imagebuilder.php?img=' + Math.random(); return false">
<img src="/common/images/refreshicon.gif" width="22" height="22" border="0" title="Refresh Security Code">
</a>
最佳答案
当您使用该语法 (.on{event} = ...
) 设置事件监听器时,您将覆盖之前为该事件设置的所有监听器。例如:
window.onload = function(){ console.log("A"); }; // Won't be executed
window.onload = function(){ console.log("B"); };
相反,您应该添加一个事件监听器,而不影响其他人:
// Both of these will be executed
window.addEventListener('load', function(){ console.log("A"); });
window.addEventListener('load', function(){ console.log("B"); });
关于javascript - 冲突的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45654162/