How to reach child element within <a> element
I have a structure like this
<ul>
<li>
<a>
<img />
<span />
</a>
</li>
<li>
......
</li&g开发者_如何学JAVAt;
<li>
......
</li>
</ul>
I am using jquery like this#
$("ul#someid").find("li:last > a[name="some.....]
How can i access the img element and the span element within the "a" element like above.
i am using now like this
$("ul#pushedProducts").find("li:last > a[name= <%=PushedProductsParametersMapper.PARAMS_PRODUCTS_INFO%>]").children('span').attr({name:'<%=PushedProductsParametersMapper.PARAMS_PRODUCTS_INFO%>' + pushedProductsTypesCount, id:'<%=PushedProductsParametersMapper.PARAMS_PRODUCTS_INFO%>' + pushedProductsTypesCount , style :'display:none;'});
If you want to select both, you can use .children()
[docs]:
$("#someid").find("li:last > a[name=['someName']").children();
Otherwise, just use the child selector [docs] again, a > b > c
,
or the descendant selector [docs], a > b c
.
You can combine the selectors [docs] however you want to.
var $img = $("ul#someid").find('li:last > a[name="some....."]').children("img")
Take care about quotes next to name
Asuming the A tag has an ID,
$('#id img')
and
$('#id span')
would be enough. Classes will work aswell using $('.class img') etc
$("ul#someid li:first a[name='somename'] img")...
$("ul#someid li:first a[name='somename'] span")...
$("ul#someid").find("li:last > a").children(); // will give you all children
$("ul#someid").find("li:last > a").find("img");
$("ul#someid").find("li:last > a").find("span");
精彩评论