使用jquery访问span

时间:2014-10-29 16:17:41

标签: jquery html sharepoint

我有这段代码:

<span id="ctl00_SiteMapPath1" >
   <span>ParentSite</span>
   <span>Site</span>
   <span>SubSite</span>
   <span>SubSubSite</span>
</span>

我想得到最后一个跨度(SubSubsite)的内部html和他父级(SubSite)的内部html, 根据创建的子网站数量自动生成跨度。

是否可以使用jQuery执行此操作? 谢谢你的帮助

4 个答案:

答案 0 :(得分:0)

这应该可以解决你的问题:

$('#ct100_SiteMapPath1').last().html();

表示最后一个并且:

$('#ct100_SiteMapPath1').last().prev().html();

之前的那个(兄弟姐妹,顺便说一下,不是父母)

参考:http://api.jquery.com/last/

答案 1 :(得分:0)

首先,您可以使用clientidmode='static',然后使用js:

$("#SiteMapPath1 span:last-child").html()//this one get html of subsubsite

$("#SiteMapPath1").children().eq(2).html()//this one get subsite

答案 2 :(得分:0)

那将是:

$('#ctl00_SiteMapPath1 > span').last().html();

&#13;
&#13;
alert( $('#ctl00_SiteMapPath1 > span').last().html() );
$('span[id]:has(span)').on('click',function() {
  alert( $(this).children('span').last().html() );
});
&#13;
span[id] {
   display:block;
   border:1px solid #000;
   margin:10px;
   padding:10px;
}
span > span {
  background-color: #c0c0c0;
  display:inline-block;
  width:24%;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="ctl00_SiteMapPath1" >
   <span>ParentSite</span>
   <span>Site</span>
   <span>SubSite</span>
   <span>SubSubSite1</span>
</span>
<span id="ctl00_SiteMapPath2" >
   <span>ParentSite</span>
   <span>Site</span>
   <span>SubSite</span>
   <span>SubSubSite2</span>
</span>
<span id="ctl00_SiteMapPath3" >
   <span>ParentSite</span>
   <span>Site</span>
   <span>SubSite</span>
   <span>SubSubSite3</span>
</span>
<span id="ctl00_SiteMapPath4" >
   <span>ParentSite</span>
   <span>Site</span>
   <span>SubSite</span>
   <span>SubSubSite4</span>
</span>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

试试这个:

var lastSpan = $("#ctl00_SiteMapPath1 span").last().html();
var beforeSpan = $("#ctl00_SiteMapPath1 span").eq(2).html();
alert("Before last span:"+beforeSpan);
alert("Last span:"+lastSpan);

工作小提琴:http://jsfiddle.net/robertrozas/nnyq4eh5/