jquery element content: html(), text(), val()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>元素的内容: html(),text(),val()</title> </head> <body> <div></div> <form> 邮箱: <input type="text" name="email"> <button>提交</button> </form> </body> </html>
html() is the same as attr(), css(), and has its own reading and setting functions depending on the parameters
1. Set element content, which can include any content: child elements or text, the same as the native innerHTML attribute
Create text node
var res = $('div').html('jQuery真的好方便')
Create element node
var res = $('div').html('<h2>jQuery真的好方便</h2>')
Get element content
var res = $('div').html()
2. Get the text content in the element: similar to the textContent attribute in the native
Get the text in a single label, the sub-element label< h2>Filter out, leaving only the text part
var res = $('div').text()
If the element content has multiple sub-elements, the text of these sub-elements will be merged
var res = $('div').html('<h2>jQuery真的好方便</h2><p>大家要好好学</p>')
Let’s use html() output to take a look
var res = 'html()输出:'+$('div').html()
Use the text() method to output again, compare the two output results, pay attention to the use of newlines in the console\n
res += '\n' + 'text()输出:' + $('div').text()
If text() has parameters, it is a setting operation
var res = $('div').text('祝愿大家早日成为一名合格的Web开发程序员')
3. Get or set the data in the form control: val(), which is equivalent to the value attribute of the form element in the native
$('button').click(function(){
Read the value of the value attribute
alert($(':text').val())
Set The value of the value attribute
$(':text').val('zhu@php.cn') alert($(':text').val()) })
Console view results
console.log(res)
The above is the detailed content of jquery element content: html(), text(), val(). For more information, please follow other related articles on the PHP Chinese website!