jquery更新和获取文本
更新和获取是同一种方法
html方法
<!DOCTYPE html>
<html>
<head>
<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(function(){
alert($("#test").html())
//html()方法用于获取文本
})
</script>
</head>
<body>
<p id="test">这是段落中的<b>粗体</b>文本。</p>
</body>
</html>
text方法
<!DOCTYPE html>
<html>
<head>
<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(function(){
$("#test").text("hello,world")
//text()方法用于更改文本
})
</script>
</head>
<body>
<p id="test">这是段落中的<b>粗体</b>文本。</p>
</body>
</html>
更新文本理论上也可以用=操作符,不过jquery选择了点操作符。然后要替换的文本就在括号里做了参数。
再来text和html方法如果有参数,就是更新文本,没参数就是获取文本。
对比一下javascript的做法
document.getElementById(id).innerHTML = something;
函数太长;此外,是更改innerHTML属性,这里jquey是HTML方法。
网友评论