jQuery:text()和html()有什么区别? [英] What is the difference between jQuery: text() and html() ?
本文介绍了jQuery:text()和html()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
jQuery中的text()和html()函数之间有什么区别?
What the difference between text() and html() functions in jQuery ?
$("#div").html('<a href="example.html">Link</a><b>hello</b>');
vs
$("#div").text('<a href="example.html">Link</a><b>hello</b>');
推荐答案
我认为差异几乎是不言自明的。
I think the difference is nearly self-explanatory. And it's super trivial to test.
http的实时演示://jsfiddle.net/hossain/sUTVg/
<html>
<head>
<title>Test Page</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#div1").html('<a href="example.html">Link</a><b>hello</b>');
$("#div2").text('<a href="example.html">Link</a><b>hello</b>');
});
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
jQuery.html()
作为HTML, jQuery.text()
将内容视为文本。
jQuery.html()
treats the string as HTML, jQuery.text()
treats the content as text.
这篇关于jQuery:text()和html()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文