4

我想从下面的代码示例中获取 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 解决方案。

谢谢, 迪甘巴特

4

8 回答 8

2

如果你想要每个li3rdspan的内容:

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/

于 2013-07-29T12:27:25.230 回答
1

这样做怎么样

spans = $('li[scriptclass="SharePointFileAttachmentContainer"]').find('span[id^="ctl00_m"]').find('span');
console.log( spans );

崩溃(适合学习)

  1. 带有 scriptclass 属性的 li 标签
  2. 跨度以 ctl00_m 开头的 id
  3. 终于找到了里面的跨度。

如果您想遍历找到的每个跨度:

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

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

于 2013-07-29T12:35:56.120 回答
1
var mySpans = $("li span span span");
于 2013-07-29T12:30:14.310 回答
0

你可以这样做 -

var spans = $('li').map(function(){
       return $(this).find('a').next('span');
});
于 2013-07-29T12:27:35.500 回答
0

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

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

在上面的代码中,它将获得第一个span内部li和内部,span它将获得最后一个span,因此您将获得span结果。

看演示

于 2013-07-29T12:27:40.240 回答
0

更优雅的解决方案:

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

查看文档

于 2013-07-29T12:28:00.783 回答
0

尝试

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

演示:小提琴

于 2013-07-29T12:28:46.620 回答
0

因为你已经上课a3_KcMvdcLcp51gEaVY_0了,li而且只有一个a锚标签。你可以简单地拥有

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

检查这个JSFiddle

于 2013-07-29T12:37:14.760 回答