addeventlistener无法在JavaScript中工作
我正在尝试将事件侦听器添加到当前文档中的所有a标记中,以便在单击a标记时将href值存储在会话中。但我编写的以下脚本甚至没有警告值“sel”addeventlistener无法在JavaScript中工作,javascript,Javascript,我正在尝试将事件侦听器添加到当前文档中的所有a标记中,以便在单击a标记时将href值存储在会话中。但我编写的以下脚本甚至没有警告值“sel” window.onload=function(){ var x=document.getElementsByTagName(“a”); 对于(var i=0;i您需要传入nextLocation函数,而不是该函数的返回,如下所示: <script type="text/javascript" > window.onload=functio
window.onload=function(){
var x=document.getElementsByTagName(“a”);
对于(var i=0;i您需要传入nextLocation函数,而不是该函数的返回,如下所示:
<script type="text/javascript" >
window.onload=function(){
var x=document.getElementsByTagName("a");
for(var i=0; i<x.length; i++){
x[i].addEventListener("click", nextlocation, false); // HERE
};
};
function nextlocation(){
sessionStorage.setItem("next","sel");
alert(sessionStorage.getItem("next"));
};
</script>
window.onload=function(){
var x=document.getElementsByTagName(“a”);
对于(var i=0;i您需要传入nextLocation函数,而不是该函数的返回,如下所示:
<script type="text/javascript" >
window.onload=function(){
var x=document.getElementsByTagName("a");
for(var i=0; i<x.length; i++){
x[i].addEventListener("click", nextlocation, false); // HERE
};
};
function nextlocation(){
sessionStorage.setItem("next","sel");
alert(sessionStorage.getItem("next"));
};
</script>
window.onload=function(){
var x=document.getElementsByTagName(“a”);
对于(var i=0;i问题在于nextLocation()
传递给addEventListener
的参数实际上是调用nextLocation
,而不是引用它,因此函数的返回值是作为回调提供给addEventListener
的值,但函数不返回任何内容,因此整个方法调用失败
因此,纠正问题很容易。从参数中删除()
因此,函数被引用,但未被调用
但是,如果您要使用DOM事件标准进行事件连接(addEventListener
),那么您应该始终如一地使用它。换句话说,不要使用.onXyz()
事件属性,就像使用window.onload
那样,因为它们不像.addEventListener()
那样健壮
此外,没有必要将type=text/javascript
添加到脚本
标记中。此外,.addEventListener
的第三个参数指定是否要使用事件捕获的默认值为false
,因此无需显式传递false
因此,代码的更正版本如下所示:
<script>
window.addEventListener("load", function(){
var x=document.getElementsByTagName("a");
for(var i=0; i<x.length; i++){
x[i].addEventListener("click", nextlocation);
};
});
function nextlocation(){
sessionStorage.setItem("next","sel");
alert(sessionStorage.getItem("next"));
};
</script>
addEventListener(“加载”,函数(){
var x=document.getElementsByTagName(“a”);
对于(var i=0;i问题在于nextLocation()
传递给addEventListener
的参数实际上是调用nextLocation
,而不是引用它,因此函数的返回值是作为回调提供给addEventListener
的值,但函数不返回任何内容,因此整个方法调用失败
因此,纠正问题很容易。从参数中删除()
因此,函数被引用,但未被调用
但是,如果您要使用DOM事件标准进行事件连接(addEventListener
),那么您应该始终如一地使用它。换句话说,不要使用.onXyz()
事件属性,就像使用window.onload
那样,因为它们不像.addEventListener()
那样健壮
此外,没有必要将type=text/javascript
添加到脚本
标记中。此外,.addEventListener
的第三个参数指定是否要使用事件捕获的默认值为false
,因此无需显式传递false
因此,代码的更正版本如下所示:
<script>
window.addEventListener("load", function(){
var x=document.getElementsByTagName("a");
for(var i=0; i<x.length; i++){
x[i].addEventListener("click", nextlocation);
};
});
function nextlocation(){
sessionStorage.setItem("next","sel");
alert(sessionStorage.getItem("next"));
};
</script>
addEventListener(“加载”,函数(){
var x=document.getElementsByTagName(“a”);
for(var i=0;i应该是addEventListener(“click”,nextlocation,false)
而不带()
nextlocation()
是nextlocation
函数调用的结果。应该是addEventListener(“click”,nextlocation,false)
而不带()
nextlocation()
是nextlocation
函数调用的结果。它工作正常,感谢您的精彩解释。它工作正常,感谢您的精彩解释。感谢兄弟的帮助,您的答案也正确。感谢兄弟的帮助,您的答案也正确。