jquery获取文档内容操作text()和html()对比
1.text() 方法返回或设置被选元素的内容,HTML 标签会被转义
This is a paragraph.
[code="jquery"] $("p").text(); //获取p的文本,结果:段落1 [/code] 也可以设置文本内容:当该方法用于设置值时,它会覆盖被选元素的所有内容。 [code="jquery"] $("p").text("Hello world!"); //重写p的内容 [/code] 结果: [img]http://dl2.iteye.com/upload/attachment/0105/2987/7c41e56a-1d2e-30e1-8eba-72b59fb55e03.png[/img] 2.html() 返回或设置被选元素的内容 ***无参数时--用于获取值,该方法仅返回第一个匹配元素的内容。 ***有参数时--用户设置值,该方法设置所有匹配元素的内容。
This is a paragraph.
This is a paragraph2.
[code="jquery"] $("p").html(); //结果:段落1 [/code] [code="jquery"] $("p").html("Hello world!");[/code] 结果: [img]http://dl2.iteye.com/upload/attachment/0105/3001/82e4d28e-9766-337c-a75e-b264b5b03ee9.png[/img] 结论:通过例子知道,html()方法与text()方法的区别就在于[color=red]对html标签的识别上不同[/color]。