Javascript 如何添加多个document.getElementById?

Javascript 如何添加多个document.getElementById?,javascript,jquery,Javascript,Jquery,以下各项工作正常: document.getElementById("comment").style.background=color 我想添加几个ID。以下操作不起作用: document.getElementById("comment, name, url").style.background=color document.querySelectorAll("comment, name, url").style.background=color 有人能建议编写什么代码来绑定所有ID的新函

以下各项工作正常:

document.getElementById("comment").style.background=color
我想添加几个ID。以下操作不起作用:

document.getElementById("comment, name, url").style.background=color
document.querySelectorAll("comment, name, url").style.background=color
有人能建议编写什么代码来绑定所有ID的新函数吗

编辑: 这是我正在研究的代码: 在标题上,我有:

<script>
function setbg(color)
{
document.getElementById("comment").style.background=color
}
</script>

功能设置(颜色)
{
document.getElementById(“注释”).style.background=color
}
并且它在以下文本区域具有良好的样式:

<p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4" required="" title="Mandatory field" onfocus="this.value=''; setbg('#e5fff3');" onblur="setbg('white')" placeholder="Add a comment here..." style="background-color: white; background-position: initial initial; background-repeat: initial initial;"></textarea></p>

但我希望它也适用于:

<input type="text" name="url" id="url" value="" size="22" tabindex="3" placeholder="WWW" onfocus="this.value=''; setbg('#e5fff3');" onblur="setbg('white')">


以及根据其他字段,如电子邮件、姓名等创建和使用功能:

function colorElement(id, color){
    document.getElementById(id).style.backgroundColor = color;
}

colorElement('comment', 'red');
colorElement('name', 'green');
colorElement('url', 'blue');

或者您可以使用元素
id
s的数组:

['comment', 'name', 'url'].forEach(function(a){
    document.getElementById(a).style.backgroundColor = 'red';
});

或者,作为前一个的发展(允许您设置不同的颜色):


.

创建并使用函数:

function colorElement(id, color){
    document.getElementById(id).style.backgroundColor = color;
}

colorElement('comment', 'red');
colorElement('name', 'green');
colorElement('url', 'blue');

或者您可以使用元素
id
s的数组:

['comment', 'name', 'url'].forEach(function(a){
    document.getElementById(a).style.backgroundColor = 'red';
});

或者,作为前一个的发展(允许您设置不同的颜色):


.

既然您标记了jQuery,下面是一种方法:

$("#comment, #name, #url").css("background-color",color);

这将获取多个ID,并将样式应用于它们。

既然您标记了jQuery,下面是一种方法:

$("#comment, #name, #url").css("background-color",color);

这将获取多个ID,并将样式应用于它们。

另一种方法是创建一个ID数组和循环数组

var els=["comment", "name", "url"];

while (els.length){
  document.getElementById(els.pop()).style.backgroundColor = color;
}

另一种方法是创建一个ID数组和循环数组

var els=["comment", "name", "url"];

while (els.length){
  document.getElementById(els.pop()).style.backgroundColor = color;
}

您可以使用元素名数组进行迭代,如

for(var i=0; i<3; i++) 
{
   document.getElementById(array[i]).style.background=color;
}

for(var i=0;i您可以使用元素名数组进行迭代,如

for(var i=0; i<3; i++) 
{
   document.getElementById(array[i]).style.background=color;
}

for(var i=0;igetElementById
方法只能获取一个元素,因此需要在每个id上使用它:

var ids = ["comment", "name", "url"];
for (i in ids) {
  document.getElementById(ids[i]).style.background = color;
}
querySelectorAll
使用一个选择器,因此您需要在每个id前面加上
#
,然后需要循环遍历结果,因为您一次只能在一个元素上设置属性:

var el = document.querySelectorAll("#comment, #name, #url");
for (i in el) {
  el[i].style.background = color;
}

演示:

getElementById
方法只能获取一个元素,因此您需要在每个id上使用它:

var ids = ["comment", "name", "url"];
for (i in ids) {
  document.getElementById(ids[i]).style.background = color;
}
querySelectorAll
使用一个选择器,因此您需要在每个id前面加上
#
,然后需要循环遍历结果,因为您一次只能在一个元素上设置属性:

var el = document.querySelectorAll("#comment, #name, #url");
for (i in el) {
  el[i].style.background = color;
}


演示:

你在哪里使用jquery?$(“#comment,#name,#url”).css('background-color',color);应该可以工作。看:JavaScript在HTML上工作,而不是PHP(请在浏览器中显示由PHP创建的HTML,'view source')。好的,我现在把干净的HTML放在哪里使用jquery?$(“#comment,#name,#url”).css('background-color',color);应该可以工作。看:JavaScript在HTML上工作,而不是在PHP上工作(请在浏览器中显示由PHP创建的HTML,“查看源代码”)。好的,我现在使用干净的HTML我相信你的意思是
colorElement()
而不是
color()
?我做到了;我在编写演示时刚刚捕捉到。谢谢!=DHmm,谢谢。但是我不需要在标题上指定颜色。我将在不同的表单上指定它……例如:
onfocus=“this.value=”;setbg(“#e5fff3”);”
。现在我在标题中使用
函数setbg(color){document.getElementById(“comment”).style.background=color}
,但我希望稍后通过
setbg
设置颜色,我真的不太确定您在问什么(顺便说一句,您似乎没有从您编写/显示的内容中使用jQuery,为什么它在标记中?);是否要更改给定元素(
表单
)的
背景颜色
当它的一个子项被聚焦?或者改变表单控件的颜色(
input
textarea
)?嗯……我在一个网站上找到了这个教程。它被称为“使用HTML5+jQuery的样式textaread”。我以为是jQuery,但我不知道。请看这里……现在代码只适用于文本区域。我想适用于所有其他字段。我相信你是指
colorElement()
而不是
color()
?我做到了;我在编写演示时刚刚捕捉到。谢谢!=DHmm,谢谢。但是我不需要在标题上指定颜色。我将在不同的表单上指定它……例如:
onfocus=“this.value=”;setbg(“#e5fff3”);”
。现在我在标题中使用
函数setbg(color){document.getElementById(“comment”).style.background=color}
,但我希望稍后通过
setbg
设置颜色,我真的不太确定您在问什么(顺便说一句,您似乎没有从您编写/显示的内容中使用jQuery,为什么它在标记中?);是否要更改给定元素(
表单
)的
背景颜色
当它的一个子项被聚焦?或者改变表单控件的颜色(
input
textarea
)?嗯……我在一个网站上找到了这个教程。它被称为“使用HTML5+jQuery的样式textaread”。我原以为是jQuery,但我不知道。请看这里……现在代码只适用于textarea。我想在数组中为..in的所有其他字段工作?@PaulS.:它迭代键,数组中的键是索引。我已经尝试了这两个字段,但在我的代码中不起作用:(可能是我做错了什么。@Pikk:我添加了一个演示的链接。@Guffa如果浏览器支持<代码>中的..,但不支持枚举性,它也可能在<代码>长度<代码>上迭代,同样,如果添加了任何非数字的可枚举键,即使它们不是“项”,也会包括在内。)。通常最好使用整数变量来迭代数组。还有一件事:您忘记了
var
它,因此您的代码当前使
i
全局化。
for..in
在数组上?@PaulS.:它迭代键,数组中的键就是索引。我尝试了这两种方法,但在我的代码中不起作用:(可能是我做错了什么。@Pikk:我添加了一个演示的链接。@Guffa如果浏览器支持<代码>中的..,但不支持枚举性,它也可能会在<代码>长度<代码>上迭代,类似地,如果添加了任何可枚举键