javascript - 如何将此值传递给 onload 函数上的 setinterval

标签 javascript jquery

我想将其发送到在图像的 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/

相关文章:

javascript - 是否有一个库函数可以在连接它找到的任何数组的同时合并对象?

javascript - 在其自己的图像后显示 alt attr

javascript - 没有时间戳参数的 jQuery 追加?

javascript - 从网站删除文本选择限制的脚本

javascript - React - 使用数据设置状态不起作用/将其设置为空数组 - 为什么?

javascript - .validate 规则 : check jquery var

jQuery/CSS 移动图标动画

javascript - 隐藏/显示使用类不起作用

javascript - 在 iframe 元素中的 srcdoc 属性中使用链接时,链接相对于什么?

javascript - 如何在客户端 (JavaScript) 上使用服务器端 (VB.NET) 的返回值?