为什么我的 onclick 没有覆盖之前执行的 js 函数?该函数从 php 中的 echo 中触发。该函数被执行并更改图像及其宽度和高度。稍后,如果我单击按钮,我希望图像再次更改,但是只有图像更改,宽度和高度不变。
<script>
function helmfunc() {
document.getElementById("helmplace").src = "helmimg/s_helmet.png"; //executes
document.getElementById("helmplace").style.width = "22px"; //executes
document.getElementById("helmplace").style.height = "31px"; //executes
}
</script>
<img id="helmplace" name="showh" src="redx3.png" width="25" height="30" />
<span onclick="showh.src = x_helm.src; showh.width = x_helm.width; showh.height = x_helm.height; helm.value = 'x helm'">
<il>
<img id="xhelmid" name="x_helm" src="helmimg/x_helmet.png" width="22" height="31" />x Helm
</il>
</span>
在一些 php 里面:(在正文的最下面)
echo '<script type="text/javascript"> slayerhelmfunc(); </script>';
最佳答案
在单击时运行代码的普遍接受的方法是使用事件监听器,而不是使用 onclick。这是一个例子:
<!DOCTYPE html>
<html>
<body>
<input type="number" id="inputInteger" />
<input type="submit" id="btnSubmit" text="Submit" />
<p id="result"></p>
<script type = "text/javascript">
// Event Listener that Runs "Compute()" on a Click
document.getElementById("btnSubmit").addEventListener("click", compute);
// Gets Input Value and Displays it in a <p>
function compute() {
var day = document.getElementById("inputInteger").value;
document.getElementById("result").innerHTML = day;
}
</script>
</body>
</html>
关于javascript - 为什么我的 onclick 不覆盖之前执行的 js 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41285971/