搜索
您的当前位置:首页正文

jquery更新和获取文本

来源:二三娱乐

jquery更新和获取文本

更新和获取是同一种方法

html方法
<!DOCTYPE html>
<html>
<head>
<script 
</script>
<script>
$(function(){
    alert($("#test").html())
    //html()方法用于获取文本
})
</script>
</head>

<body>
<p id="test">这是段落中的<b>粗体</b>文本。</p>
</body>

</html>

text方法

<!DOCTYPE html>
<html>
<head>
<script 
</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方法。

参考

Top