我想将其发送到在图像的 onload 事件上使用 setInterval 调用的函数,但它会引发以下错误:
Uncaught TypeError: Cannot read property 'attr' of undefined(…)
<img src="image.jpg" id="24" onload="var sel =this;setInterval(function(sel){calllogin(sel)},20000);"
<script>
function calllogin(sel){
var id = sel.attr("id");
console.log('calling calllogin function',id);
}
</script>
我怎样才能通过这个?
最佳答案
您必须从函数参数中删除 setInterval(function(sel)
, sel
。
onload="var sel =this; setInterval(function(){ calllogin(sel) },20000);"
或者只是将 sel
作为第三个参数传递给 setInterval
setInterval(function(sel){calllogin(sel)},20000, sel);
并且callBack中的sel
将不是jquery对象。它将是一个简单的元素对象。您必须在使用 Jquery 函数之前对其进行转换。
function calllogin(sel) {
var id = $(sel).attr("id");
console.log('calling calllogin function',id);
}
关于javascript - 如何将此值传递给 onload 函数上的 setinterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40840764/