简体   繁体   中英

How to reach child element within <a> element

I have a structure like this

 <ul>
  <li>
    <a>
     <img />
     <span />
    </a>
  </li>
  <li> 
    ......
  </li>
  <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 > bc .

You can combine the selectors [docs] however you want to.

var $img = $("ul#someid").find('li:last > a[name="some....."]').children("img")

注意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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM