我有一个代码可以以特定排列显示图像以反射(reflect)晶圆。当用户单击图像时,会在其周围放置一个红色边框。这个图像 id 然后保存在一个 cookie 数组中,当页面重新加载时,这些特定图像需要保留它们的边框。
我的问题是我无法使用 id 访问 html 元素。
比如这个:
$("#10.3_6").css('border', "solid 2px red");
不起作用。这就是我放在边框上的方式:
$("tr.smallthumbs a img").click(function() {
var found = 0;
for(var j in array){
if(this.id == array[j]){
found = j+1;
break;
};
};
if(found == 0){
$(this).css('border', "solid 2px blue");
array.push(this.id);
console.log(this)
}
else {
$(this).css('border', "solid 2px white");
array.splice(found-1,1)
};
$.cookie('activePixels',array);
});
这会在所有图像周围放置一个红色边框。
$("tr.smallthumbs a img").css('border', "solid 2px red");
如何通过 ID 访问一个?
谢谢!
最佳答案
选择器
$("#10.3_6")
表示具有id="10"
和class="3_6"
的元素。如果你想访问带有 id="10.3_6"
的元素,你需要转义 。
:
$("#10\\.3_6")
最好为您的 ID 使用不同的命名方案,因为 .
在选择器中具有这种特殊含义。
关于javascript - 使用 Jquery 按类和 ID 获取图像元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22239124/