0

我深深的疑惑...

大约一天前,我发布了一个关于使用附加到元素的 idletimer 的查询,以便在页面上特定元素的活动/不存在活动时触发或不触发事件,如下所示:

$("#testme"  ).on( "idle.idleTimer", function(event, elem, obj){ $("#test").html("I am very idle");});
$( "#testme"  ).on( "active.idleTimer", function(event, elem, obj, triggerevent){ $("#test").html("not idle");});
$( "#testme" ).idleTimer(3000);

似乎我已经完成了这项工作:您可以在http://jsfiddle.net/peterrr73/C3w7f/3中看到它运行良好。但是,当我将相同的代码粘贴到单独的文件中并将其放到网络上时,它会失败:您可以在http://www.sd-editions.com/CantApp/index4.html看到这个。更令人费解的是:您可以在http://www.sd-editions.com/CantApp/index2.html上看到,当附加到整个文档时,惰轮正在工作。

到底是怎么回事?当我去在线不运行的实例时没有报告错误。

4

1 回答 1

1
  • jsfiddler 运行onLoad,因此所有主体节点都已加载
  • dom elements未加载的第一个链接使用
  • 第二个链接将事件附加到document

这是您的解决方案(使用选择器包装代码):

$(function() {
/* your code goes here */
});
于 2014-03-27T16:55:16.510 回答