如何使用jQuery获取嵌套<span>的值?</span>

时间:2013-07-29 12:24:05

标签: jquery html

我想从下面的代码示例中获取Span值。

<ul style="list-style:none;padding:0px;margin:0px;">
  <li scriptclass="SharePointFileAttachmentContainer" class="a3_KcMvdcLcp51gEaVY_0" id="ctl00_m_g_92fd70a8_aeaf_433d_8f82_e1ead1630fec_FormControl0_V1_I1_SPFAC26_SPI124" formid="ctl00_m_g_92fd70a8_aeaf_433d_8f82_e1ead1630fec_FormControl0" originalid="V1_I1_SPFAC26_SPI124" viewdatanode="38">
    <span style="" onmouseover="return LeafControl.OnWrappingSpanMouseOver(this, event);" onmouseout="return LeafControl.OnWrappingSpanMouseOut(this, event);">
      <span id="ctl00_m_g_92fd70a8_aeaf_433d_8f82_e1ead1630fec_FormControl0_V1_I1_SPFAC26_SPI124_SPFA1" scriptclass="SharePointFileAttachmentItem" class="b_KcMvdcLcp51gEaVY_0 a2_KcMvdcLcp51gEaVY_0 bh_KcMvdcLcp51gEaVY_0" direction="ltr" viewdatanode="39" formid="ctl00_m_g_92fd70a8_aeaf_433d_8f82_e1ead1630fec_FormControl0" originalid="V1_I1_SPFAC26_SPI124_SPFA1" style="margin:0px; width: 100%; display:inline-block;">
        <a href="#" style="text-decoration:none; border:none;" onclick="return (SharePointFileAttachmentItem.OnClick(this, event));" onfocus="return (SharePointFileAttachmentItem.OnFocus(this, event));" onkeypress="return (SharePointFileAttachmentItem.OnKeyPress(this, event));">
          <img title="Click here to delete the file. EAMS Masters.docx" alt="Click here to delete the file. EAMS Masters.docx" style="cursor: hand;border:none;vertical-align:middle;margin-right:1px;" src="/_layouts/inc/ipfsfileattachmentdelete.png?rev=yS8PMFXDZDJFrumpJ9CThg%3D%3D">
        </a>
        <span>EAMS Masters.docx</span>
      </span>
    </span>
  </li>
  <li scriptclass="SharePointFileAttachmentContainer" class="a3_KcMvdcLcp51gEaVY_0" id="ctl00_m_g_92fd70a8_aeaf_433d_8f82_e1ead1630fec_FormControl0_V1_I1_SPFAC26_SPI125" formid="ctl00_m_g_92fd70a8_aeaf_433d_8f82_e1ead1630fec_FormControl0" originalid="V1_I1_SPFAC26_SPI125" viewdatanode="40">
    <span style="" onmouseover="return LeafControl.OnWrappingSpanMouseOver(this, event);" onmouseout="return LeafControl.OnWrappingSpanMouseOut(this, event);">
      <span id="ctl00_m_g_92fd70a8_aeaf_433d_8f82_e1ead1630fec_FormControl0_V1_I1_SPFAC26_SPI125_SPFA1" scriptclass="SharePointFileAttachmentItem" class="b_KcMvdcLcp51gEaVY_0 a2_KcMvdcLcp51gEaVY_0 bh_KcMvdcLcp51gEaVY_0" direction="ltr" viewdatanode="41" formid="ctl00_m_g_92fd70a8_aeaf_433d_8f82_e1ead1630fec_FormControl0" originalid="V1_I1_SPFAC26_SPI125_SPFA1" style="margin:0px; width: 100%; display:inline-block;">
        <a href="#" style="text-decoration:none; border:none;" onclick="return (SharePointFileAttachmentItem.OnClick(this, event));" onfocus="return (SharePointFileAttachmentItem.OnFocus(this, event));" onkeypress="return (SharePointFileAttachmentItem.OnKeyPress(this, event));">
          <img title="Click here to delete the file. 123.docx" alt="Click here to delete the file. 123.docx" style="cursor: hand;border:none;vertical-align:middle;margin-right:1px;" src="/_layouts/inc/ipfsfileattachmentdelete.png?rev=yS8PMFXDZDJFrumpJ9CThg%3D%3D">
        </a>
        <span>123.docx</span>
      </span>
    </span>
  </li>
</ul>

从上面的代码示例中,我想要<span>EAMS Masters.docx</span><span>123.docx</span>这个跨度值。 我有多个<li>这样的人。

所以请在Jquery或Javascript中建议我解决方案。

谢谢, Digambart

8 个答案:

答案 0 :(得分:2)

如果您想要每个li的第3个span内容:

var i=1;
$('ul li').each(function(){
    var span_val=$(this).find('span span span').text();
    //code for doing something with span_val
});

这里的工作示例:http://jsfiddle.net/s3C2X/1/

答案 1 :(得分:1)

var mySpans = $("li span span span");

答案 2 :(得分:1)

怎么样这样做

spans = $('li[scriptclass="SharePointFileAttachmentContainer"]').find('span[id^="ctl00_m"]').find('span');
console.log( spans );
分解(有利于学习)

  1. li标签包含scriptclass
  2. 的属性
  3. 跨越以ctl00_m
  4. 开头的ID
  5. 终于找到了里面的跨度。
  6. 如果你想遍历找到的每个跨度:

    spans.each(function(){
        console.log( $(this).text() );
    });
    

    工作样本 http://jsfiddle.net/fedmich/HdMTH/

答案 3 :(得分:0)

你可以这样做 -

var spans = $('li').map(function(){
       return $(this).find('a').next('span');
});

答案 4 :(得分:0)

您可以使用以下代码执行此操作。

$('li').each(function(){
   $(this).find('span span:last').css('border','1px solid red'); 
});

在上面的代码中,它将获得span内的第一个li,而span内的span将获得最后span,因此您将{{1}}作为{{1}}结果

SEE DEMO

答案 5 :(得分:0)

更优雅的解决方案:

$('li span:nth-child(3)')

查看the docs

答案 6 :(得分:0)

尝试

$('li.a3_KcMvdcLcp51gEaVY_0').find('span span span').each(function(){
    console.log($(this).text())
})

演示:Fiddle

答案 7 :(得分:0)

由于您为a3_KcMvdcLcp51gEaVY_0上课li,并且有一个a锚标记。你可以简单地拥有

   $('.a3_KcMvdcLcp51gEaVY_0 a').next('span').text();

检查此JSFiddle