Answers:
我认为这应该是一个简单的例子:
$('#item1 span').text();
要么
$('#item1 span').html();
.text()
它将对找到的所有字符进行html编码,而不.html()
会对任何字符进行编码。
$('.class').each(function() { $(this).html() });
; 因为$('.class').html()
它将返回第一个元素的.html()值
由于您没有为“ item”值提供属性,因此我假设正在使用一个类:
<div class='item1'>
<span>This is my name</span>
</div>
alert($(".item span").text());
确保您等待DOM加载以使用您的代码,在jQuery中,您ready()
为此使用了函数:
<html>
<head>
<title>jQuery test</title>
<!-- script that inserts jquery goes here -->
<script type='text/javascript'>
$(document).ready(function() { alert($(".item span").text()); });
</script>
</head>
<body>
<div class='item1'>
<span>This is my name</span>
</div>
</body>
$('#item1 span').html();
它与我的代码一起工作