本文摘自PHP中文网,作者不言,侵删。
本篇文章给大家带来的内容是关于JQuery中text(),html()和val()之间有何区别?有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。定义和用法
text() 方法方法设置或返回被选元素的文本内容
代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!DOCTYPE html>
<html>
<head>
<meta charset= "UTF-8" >
<meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
<meta http-equiv= "X-UA-Compatible" content= "ie=edge" >
<script src= "./js/jquery-1.12.4.js" ></script>
<title>Document</title>
</head>
<body>
<div id= "div1" >div有文本内容</div>
<div id= "div2" >
div2内的文本
<span>span内有文本内容</span>
</div>
<input type= "text" id= "input1" value= "这是一个input标签" >
<input type= "text" name= "" id= "input2" placeholder= "能成功获取" >
<button id= "button1" value= "这是一个button标签" ></button>
<script>
console.log($( "#div1" ).text());
console.log($( "#div2" ).text());
console.log($( "#div2 span" ).text()) ;
console.log($( "#input1" ).text());
console.log($( "#input2" ).text());
console.log($( "#button1" ).text());
</script>
</body>
</html>
|
console打印的结果
可以看出text()只输出标签内的文本内容,和js的innerText方法一样
定义和用法
html() 方法返回或设置被选元素的内容 (inner HTML),包括标签。
如果该方法未设置参数,则返回被选元素的当前内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <body>
<div id= "div1" >div有文本内容</div>
<div id= "div2" >
div2内的文本
<span>span内有文本内容</span>
</div>
<input type= "text" id= "input1" value= "这是一个input标签" >
<input type= "text" name= "" id= "input2" placeholder= "能成功获取" >
<button id= "button1" value= "这是一个button标签" ></button>
<script>
console.log($( "#div1" ).html());
console.log($( "#div2" ).html());
console.log($( "#div2 span" ).html());
console.log($( "#input1" ).html());
console.log($( "#input2" ).html());
console.log($( "#button1" ).html());
</script>
</body>
|
通过console的打印的结果
打印当前标签内的文本内容,如果有子标签,则把子标签本身和子标签内的文本一起打印
这个和js的innerHTML差不多
定义和用法
val() 方法返回或设置被选元素的值。
元素的值是通过 value 属性设置的。该方法大多用于 input 元素。
方法主要用于获取表单元素的值
如果该方法未设置参数,则返回被选元素的当前值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <body>
<div id= "div1" >div有文本内容</div>
<div id= "div2" >
div2内的文本
<span>span内有文本内容</span>
</div>
<input type= "text" id= "input1" value= "这是一个input标签1" >
<input type= "text" name= "" id= "input2" value= "这是一个input标签2" placeholder= "能成功获取" >
<button id= "button1" value= "这是一个button标签" ></button>
<script>
console.log($( "#div1" ).val());
console.log($( "#div2" ).val());
console.log($( "#div2 span" ).val());
console.log($( "#input1" ).val());
console.log($( "#input2" ).val());
console.log($( "#button1" ).val());
</script>
</body>
|
通过控制台打印结果
val()是用来输出表单内的数据,可以看出p和span标签内的文本并没有被输出,我还测试了H5新标签placeholder
同样也没有被输出,所以这个val应该是只针对标签的value属性的
本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的jquery视频教程栏目!
以上就是JQuery中text(),html()和val()之间有何区别?的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
学习使用javascript实现utf-8编解码
javascript b包是什么
使用new操作符实例化一个对象的具体步骤是什么
javascript之ssm+vue前后端分离框架整合实现
如何实现网页标题闪烁效果
一纸搞懂js系列(1)之编译原理,作用域,作用域链,变量提升,暂时性死区
javascript简称什么
javascript核心对象有哪些
javascript中如何获取当前时间
javascript怎么实现秒转时间
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » JQuery中text(),html()和val()之间有何区别?