假设我有一个事件附加到一个元素,但我想在它触发时更新另一个元素。
var el = document.getElementById("el"),
el1 = document.getElementById("el1");
el.onmouseover = function() {
// Here I want to update for example the innerHTML property
// of el1 but el1 is undefined in this scope
}
如何实现这一目标?
最佳答案
并非如此。 el1
确实是在该范围内定义的。如果看起来并非如此,那么您可能在某个地方出现了错误。甚至使用以下 HTML 对其进行了测试:
<p id=el>el
<p id=el1>el1
<script>
var el = document.getElementById("el"),
el1 = document.getElementById("el1");
el.onmouseover = function() {
alert(el1);
}
</script>
关于javascript - 更新事件触发时的另一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1987390/