简体   繁体   English

如何在元素内到达子<a>元素</a>

[英]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# 我正在使用像这样的jQuery#

            $("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. 我如何才能像上述在“ a”元素中访问img元素和span元素。

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] : 如果要选择两者,则可以使用.children() [docs]

$("#someid").find("li:last > a[name=['someName']").children();

Otherwise, just use the child selector [docs] again, a > b > c , 否则,只需再次使用子选择器[docs]a > b > c
or the descendant selector [docs] , a > bc . 后代选择器[docs]a > bc

You can combine the selectors [docs] however you want to. 您可以根据需要组合选择器[docs]

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

注意name旁边的引号

Asuming the A tag has an ID, 假设A标签有一个ID,

$('#id img')

and

$('#id span')

would be enough. 足够了。 Classes will work aswell using $('.class img') etc 使用$('。class img')等类也可以正常工作

$("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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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