动态ID的jQuery选择器 [英] JQuery Selector for dynamic id
问题描述
我正在尝试更改Jquery中的图像来源
I am trying to change the image source in the Jquery
<a href="" class="wanted" id="'.$status_id[$num].'"><img src="/images/wanted.png">
通过JQuery选择器:
through a JQuery selector:
$(".wanted "+ id).attr("src", '/images/wanted_.png');
其中id
在javascript中定义为php变量$status_id[$num]
.我首先尝试使用$(this)
无济于事.任何见解都会有所帮助.
Where id
is defined in the javascript as the php variable $status_id[$num]
. I first tried using $(this)
to no avail. Any insight would be helpful.
推荐答案
访问$(".wanted"+id)
时,实际上是在尝试访问具有类名= wanted + id的元素.这是因为."在想要"之前.另外,您似乎正在直接访问<a>
标记并将其设置为src
属性.您需要访问<img>
标记.
您可以尝试的是这样:
When you access $(".wanted"+id)
, you are actually trying to access an element with the class name = wanted+id. This is because of the '.' before 'wanted'. Also, you seem to be accessing the <a>
tag directly and setting it's src
attribute. You need to access the <img>
tag.
What you could try is this:
var x=document.getElementById(id);
$(x).find("img")[0].setAttribute("src","/images/wanted_.png");
这篇关于动态ID的jQuery选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!