我只是写了一个小代码,应该改变 X ids 的 CSS。
我在 HTML 对象中有 3 个 id(每个元素都有 1 个类)=
element 1 id = "p1" class="test1"
element 2 id = "p2" class="test1"
element 3 id = "p3" class="test1"
我创建了两个数组:
var statArray = ["p1","p3"];
var array = ["p1","p3"];
比我检查是否数组 1 = 数组 2
for (var ix = 0; ix < array.length; ix++) {
if (array[ix] === statArray[ix])
现在我将更改 id 的 css(背景颜色)。
如何更改 id = array[ix] 中的 id?
我的尝试:
$(".test1").attr(array[ix]).css("background-color","#ff0000");
最佳答案
您可以使用 ID 选择器 #
或名称-值选择器:
$("#"+array[ix]).css("background-color","#ff0000");
或
$("[id="+array[ix]+"]").css("background-color","#ff0000");
关于javascript - 如何更改 CSS where id=array(index)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23871894/