【问题标题】:How pass this value to setinterval on onload function如何将此值传递给 onload 函数上的 setinterval
【发布时间】:2016-11-28 09:20:07
【问题描述】:

我想将此发送到在图像的 onload 事件中使用 setInterval 调用的函数,但它会引发以下错误:

未捕获的类型错误:无法读取 undefined(...) 的属性“attr”

<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函数回调参数中删除sel,即setInterval(function(){calllogin(sel)},20000)
  • 另外,你缺少一个 } 来关闭你的函数 calllogin
  • @Satpal 我删除了它,现在它显示以下错误; Uncaught TypeError: sel.attr is not a function(...)
  • $(sel).attr("id"); 阅读@Rajaprabhu 答案
  • @Satpal 谢谢兄弟,它有效。

标签: javascript jquery


【解决方案1】:

您必须从函数参数中删除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);
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多