在 jQuery 中的动态对象上使用 .each?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17451616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using .each on dynamic objects in jQuery?
提问by o_O
There are lots of questions that seem to be asking this, but in the end all they want is to attach .click events and not .each and so the generally accepted answer in all of them include $("body").on("click", ".selector", function(){});
and this is definitely not what I want.
有很多问题似乎在问这个,但最终他们想要的只是附加 .click 事件而不是 .each ,因此所有这些都包括普遍接受的答案$("body").on("click", ".selector", function(){});
,这绝对不是我想要的。
I have some generated inputs and I need to change the value of each one at the same time. Normally I would $(".inputs").each(function(){$(this).val("new-value")};
or something along those lines, however, I can't because of the dynamic aspect.
我有一些生成的输入,我需要同时更改每个输入的值。通常我会$(".inputs").each(function(){$(this).val("new-value")};
或类似的东西,但是,由于动态方面,我不能。
So how would you do a $("body").on("each", ".inputs", function(){});
?
那么你会怎么做$("body").on("each", ".inputs", function(){});
呢?
采纳答案by o_O
Actually, it's as simple as running the .each inside setTimout.
实际上,它就像在 setTimout 中运行 .each 一样简单。
setTimeout(function(){
$(".inputs").each(function(){$(this).val("new-value")});
}, 5000);
回答by Samuel Reid
$.each
works with any new elements added.
$.each
适用于添加的任何新元素。
Doing something different each time:
每次都做不同的事情:
回答by Tarmo Saluste
Here is much better script that does exactly what the OP asked.
这是更好的脚本,它完全符合 OP 的要求。
function doWithInput(i, e){
$(e).val("done with each");
}
$(document).ready(function(){
$("#button").click(function(){
$("#inputs").append("<input class='inputs_new' type='text' /><br />");
});
$(".inputs").each(doWithInput);
});
$(document).on("DOMNodeInserted", ".inputs_new", function(e) {
$(e.target).removeClass('inputs_new').addClass('inputs');
doWithInput(0, e.target);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="inputs">
<input class="inputs" type="text" placeholder='type in me' /><br />
</div>
<button type="button" id="button">Add</button>
回答by T.S.
You can use $(".inputs").each(function(){$(this).val("new-value")};
perfectly fine on dynamic elements. You just need to rerun the script after you added an element dynamically.
您可以$(".inputs").each(function(){$(this).val("new-value")};
在动态元素上完美使用。您只需要在动态添加元素后重新运行脚本。
From the comments I figured that some misunderstanding exists about 'each'. 'Each' is not an event and so no event is fired if 'each' is called. If you want to keep track of added elements because you have no control over when they are added you'd need a timer:
从评论中,我认为对“每个”存在一些误解。“每个”不是事件,因此如果调用“每个”,则不会触发任何事件。如果您想跟踪添加的元素,因为您无法控制添加它们的时间,则需要一个计时器:
setInterval(function(){ $(".inputs").each(function(){$(this).val("new-value")}; },500);
回答by SIDU
What about this
:
怎么样this
:
I want the dynamically generated text to take effect on each
我希望动态生成的文本对每个文本生效