<div>
<div>
<div>1</div>
</div>
<div></div>
<div>
<div></div>
<div>
<div>2</div>
</div>
</div>
</div>
就上面的情况,怎样用jquery获取到1,2这个数字
<div>
<div>1</div>
</div>
<div></div>
<div>
<div></div>
<div>
<div>2</div>
</div>
</div>
</div>
就上面的情况,怎样用jquery获取到1,2这个数字
一般情况下,要么是层次结构清楚,要么是有特殊标记,虽然你这个可以做到,但是我想象不到有什么意义$("div").contents().not("[nodeType=1]").val()
<div>
<div>
<div>1</div>
</div>
<div></div>
<div>
<div></div>
<div>
<div>2</div>
</div>
</div>
</div>
$("#"+id).text();
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript" src="jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){alert($("div").contents().not("[nodeType=1]").val());
})
</script>
</head><body>
<div>
<div>
<div>1</div>
</div>
<div></div>
<div>
<div></div>
<div>
<div>2</div>
</div>
</div>
</div></body>
</html>
输出undefined
<script>
$(document).ready(function () {
// 1节点可以直接访问到
alert($('div>div>div').html()); // 2节点只能用循环去访问的吧。
$('div>div').each(function () {
//alert('test');
if ($(this).html() == '2')
{
alert('找到2节点');
}
});
});
</script>
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text ml; charset=gb2312" />
<title>图片滚动</title>
<script src="js/jquery.js"></script>
<script>
$(document).ready(function () {
// 1节点可以直接访问到
alert($('div>div>div').html()); // 2节点只能用循环去访问的吧。
$('div>div').each(function () {
//alert('test');
if ($(this).html() == '2')
{
alert('找到2节点');
$(this).html('第二处');
}
});
});
</script>
</head>
<body>
<div>
<div>
<div>1</div>
</div>
<div></div>
<div>
<div></div>
<div>
<div>2</div>
</div>
</div>
</div></body>
<html>
本来你这html写法就有问题的,讨论起来没什么意义
<head>
<script src="jquery-1.3.2.min.js" ></script><script>
$(function(){
$("div").each(function(){
if($(this).text().indexOf("1")>=0||$(this).text().indexOf("2")>=0)
alert($(this).text());
})
})
</script>
</head>
<body>
<div>
<div>
<div>1</div>
</div>
<div></div>
<div>
<div></div>
<div>
<div>2</div>
</div>
</div>
</div>
</body>
</html>
<head>
<script src="jquery-1.3.2.min.js" ></script><script>
$(function(){
$("div").each(function(){
if($(this).text().indexOf("1")>=0||$(this).text().indexOf("2")>=0)
alert($(this).text()+$(this).attr("id"));
})
})
</script>
</head>
<body>
<div id="div1">
<div id="div1_1">
<div id="div1_1_1">1</div>
</div>
<div id="div1_2"></div>
<div id="div1_3">
<div id="div1_3_1"></div>
<div id="div1_3_2">
<div id="div1_3_2_1">2</div>
</div>
</div>
</div>
</body>
</html>
可以看下each 的遍历顺序,囧