我已经找了好几个小时了,为什么下面的代码不起作用:
naam = "div#" + $(naam).next('div').attr('id');
current = "img." + $(naam).attr('id');
$(current).css("border", "solid 4px #9966FF");
div 的 id 与 img 中的类同名:
<div id="michiel">
这是我想要使用代码的图像:
<img src="Images/michielsmall.jpg" class="michiel, link" alt="Michiel">
如您所见,我正在尝试使用类“michiel”
更改img的边框。
当我console.log(current);
时,输出是:img.michiel
,这是正确的。
我真的不知道为什么这不起作用,请帮忙。
最佳答案
这是因为类名中的逗号
class="michiel, link"
^^------- Remove this
应该是
class="michiel link"
或者..如果您希望出现逗号,请将选择器更改为
var current = "img." + $(naam).attr('id') + "\\,";
但不建议这样做
<强> Check Fiddle
关于带有 .css 类的 jQuery img 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13958341/