使用 Javascript (jQuery) 遍历所有 Textareas
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6201676/
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
Iterating through all Textareas with Javascript (jQuery)
提问by Jeff
I would like to perform something with all my text areas, however selecting all of them with $("textarea")
wont do. Here's what I am trying to do in Pseudo code:
我想对我所有的文本区域执行某些操作,但是选择所有文本区域是$("textarea")
行不通的。这是我在伪代码中尝试做的事情:
for each textarea do
alert(textarea.val)
In my case, I need to do a Word replacement in all of my textareas, so I thought doing it with iteration would be a lot cleaner, however I cant figure out how to do it.
就我而言,我需要在所有文本区域中进行 Word 替换,因此我认为通过迭代进行替换会更清晰,但是我不知道该怎么做。
Here is what I currently do, which is very tedious:
这是我目前所做的,这是非常乏味的:
var txtcode = $("#txt_banner1").text().replace("AFFURL",producturl);
$("#txt_banner1").text(txtcode);
var txtcode = $("#txt_banner2").text().replace("AFFURL",producturl);
$("#txt_banner2").text(txtcode);
... and on and on....
……等等……
Thanks!
谢谢!
回答by rahul
$(function(){
$("textarea").each(function(){
this.value = this.value.replace("AFFURL",producturl);
});
});
See a working demo
查看工作演示
回答by Loktar
回答by diEcho
instead using id do below
而是使用下面的 id 做
$("textarea").each ( function () {
// whatever u want
});
回答by netbrain
//alternative 1
$(function() {
$('textarea').each(function() {
alert($(this).text())
})
})
//alternative 2 only those who has specific class
$(function() {
$('textarea.myTextArea').each(function() {
alert($(this).text())
})
})
//alternative 3, handpicked textareas
$(function() {
var handPicked = ['#2', '.myTextArea']
for (x = 0; x < handPicked.length; x++) {
alert($(handPicked[x]).text())
}
})