我可以在文档上使用什么来不时应用函数,包括未来元素
What can I use on the document to apply a function from time to time including future elements?
我有一些HTML片段是在用户单击按钮时添加的,我希望不时对添加的片段进行更改。我不能使用$(document).ready()
,因为当页面准备好/加载时,代码的这些部分还没有出现。
我可以在文档上使用什么来不时地将函数应用到DOM(包括未来的元素)?类似于:
$(document).atEach(3000, function() {
// As the user clicks a button, a field is added to the page with a class.
// The "window" applies every 3 seconds the given function within the scope of all the DOM,
// (It updates the elements that were inserted)
if ($(".class").val().length) {
$(".class").removeClass("input-red").addClass("input-green");
} else {
$(".class").addClass("input-red").removeClass("input-green");
}
});
要以规则的时间间隔执行函数,可以使用setInteval
setInterval(function() {
// As the user clicks a button, a field is added to the page with a class.
// The "window" applies every 3 seconds the given function within the scope of all the DOM,
// (It updates the elements that were inserted)
if ($(".class").val().length) {
$(".class").removeClass("input-red").addClass("input-green");
} else {
$(".class").addClass("input-red").removeClass("input-green");
}
}, 3000);
http://jsfiddle.net/ma7p3hcr/1/
我不确定你的函数应该做什么,但我想这是某种输入验证。如果是这种情况,那么您应该只监听oninput
事件并执行它。事件委派使在动态添加的输入上侦听事件成为可能:
$(document).on('input', '.class', function(){
if( $(this).val().length ){
$(this).removeClass("input-red").addClass("input-green");
} else {
$(this).addClass("input-red").removeClass("input-green");
}
});
http://jsfiddle.net/tnc84mhf/
相关文章:
- 如何将此函数应用于以下JsonML中的每个匹配元素
- 将函数应用于页面上的所有链接
- 将函数应用于填充有数字的对象
- 将jQuery事件函数应用于新的Knockout.js数组元素
- 如何将函数应用于两个数组的第 n 个元素(数组的数组)
- 未定义角度 ng 重复函数应用函数
- 如何将此代码从 2 个函数应用到 1 个函数
- 将 javascript 函数应用于相同的 id
- 有没有一种纯粹的Javascript方法可以将一个函数应用于多个元素的事件
- 如何将Javascript单击函数应用于没有类的元素
- 如何将函数应用于下拉列表
- snap.svg:将函数应用于许多对象
- 无法将 jquery 效果函数应用于动态创建的元素
- 通过函数应用带有挖空的 % 样式绑定不起作用
- 如何将一个函数应用于网页上的多个 Id
- 如何将函数应用于完成 jquery ajax 提交的类
- 如何使onEdit()触发器函数应用于多个工作表
- RequireJS-将构造函数应用于模块
- 如何将javascript函数应用于具有特定域的href
- 将jQuery函数应用于表行,但不应用于第一个表数据