jQuery nested selector for an 'src' image URL
I have the below HTML structure.
<td id="123"&开发者_如何学编程gt;
...
<td>
<ul>
<li class"open"><img src="path" /></li>
...
</ul>
</td>
</tr>
<td id="125">
...
I want to do this:
select
li.open
of #123 and change the class toli.pending
select the
src
attribute of the#123>li.open
image and change it to "newpath"
I tried to select them by:
$('#123>li.open img').attr("src");
...to add the new src
$('#123>li.open').removeClass("open").addClass("pending");
How can I fix this problem?
The >
CSS operator enforces a direct parent-child relationship. You have a <td>
and <ul>
element in between.
Remove the >
and all should be fine.
[and per comments, for backwards compatibility don't use numbers for IDs].
Your CSS-selector says: find li.open
as a direct child of #123, but that's never true, because you have at least a td
, ul
under your #123
element.
$('#123 li.open>img').attr("src");
is more true in the markup you have posted above. img
is a direct child of li.open
.
精彩评论