简体   繁体   English

如何添加不仅仅是document.getElementById?

[英]How to add more than document.getElementById?

the following is working fine: 以下工作正常:

document.getElementById("comment").style.background=color

I'd like to add several IDs. 我想添加几个ID。 The following do not work: 以下不起作用:

document.getElementById("comment, name, url").style.background=color
document.querySelectorAll("comment, name, url").style.background=color

Can someone suggest what code avoiding to write a new function to bind all the ids? 有人可以建议什么代码避免编写新函数来绑定所有id吗?

EDIT: This is the code I am working on: On the header I have: 编辑:这是我正在工作的代码:在头上我有:

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

And it styles well the following textarea: 它很好地样式了以下文本区域:

<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>

But I'd like it to work also for: 但我希望它也可以用于:

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

As well as per other fields, like email, name, etc. 以及其他字段,例如电子邮件,姓名等。

Create, and use, a function: 创建和使用功能:

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

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

JS Fiddle demo . JS小提琴演示

Or you can use an array of element id s: 或者,您可以使用元素id的数组:

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

JS Fiddle demo . JS小提琴演示

Or, as a development of the previous (which allows you to set different colours): 或者,作为前一个的发展(它允许您设置不同的颜色):

[{
    "id": "comment",
    "color": "red"
}, {
    "id": "name",
    "color": "green"
}, {
    "id": "url",
    "color": "blue"
}].forEach(function (a) {
    document.getElementById(a.id).style.backgroundColor = a.color;
});

JS Fiddle demo . JS小提琴演示

Since you tagged jQuery, here's a way: 既然您标记了jQuery,这是一种方法:

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

This grabs multiple ids, and applies the style to them. 这将获取多个id,并将样式应用于它们。

The getElementById method can only get one element, so you would need to use it on each id: getElementById方法只能获取一个元素,因此您需要在每个id上使用它:

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

The querySelectorAll takes a selector, so you would need to prefix each id with # , then you need to loop through the result as you can only set a property on one element at a time: querySelectorAll具有一个选择器,因此您需要在每个ID前面加上#前缀,然后需要遍历结果,因为您一次只能在一个元素上设置一个属性:

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

Demo: http://jsfiddle.net/Guffa/B3J4a/ 演示: http//jsfiddle.net/Guffa/B3J4a/

Another approach is create an array of ID's and loop over array 另一种方法是创建一个ID数组并遍历该数组

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

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

Either you can iterate with array of element name like 您可以使用元素名称数组进行迭代,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM