提供两种方法获取div下第一个或最后一个a标签的内容:
使用选择器 first-child 和 last-child
使用遍历方法 first() 和last()
实例演示如下:
1、HTML结构
2、jquery代码
$(function(){
$("#btn1").click(function() {
var str = $("#test a:first-child").text();
alert(str);
});
$("#btn2").click(function() {
var str = $("#test a").last().text();
alert(str);
});
});
3、效果演示
第一个:$(".frameswitch > a:first-child").text()
最后一个:$(".frameswitch > a:last-child").text()
子元素和后代元素要区分一下。