Home > Article > Web Front-end > Can jquery clear text content?
jquery can clear text content. Clearing method: 1. Use text() to clear the text content of ordinary elements. The syntax is "element object.text("")". Just set the text content to nullable characters; 2. Use val() to clear the text of the form element input. Content, syntax "$("input").val("")".
The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.
jquery can clear text content.
There are two cases for clearing text content:
Clear the text content of ordinary elements
Clear form elements The text content of input
1. Use text() to clear the text content of ordinary elements
text() can set the text content of the element. Just set the text content to nullable characters to clear it.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { $("p").text(""); }); }); </script> </head> <body> <button>清除所有p元素的文本内容</button> <p>这是一个段落。</p> <p>这是另一个段落。</p> </body> </html>
2. Use val() to clear the text content of the form element input
val() method returns or sets the selected The value of the element. The value of an element is set via the value attribute. This method is mostly used for input elements.
Just use val() to set the text content to nullable characters to clear.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { $("input").val(""); }); }); </script> </head> <body> <button>清除所有input元素的文本内容</button> <p>用户名: <input type="text" name="user" value="李华" /></p> <p>密 码: <input type="password" name="password" value="123456" /></p> </body> </html>
Extended knowledge:
To set the element content, there is also a method html().
But the content set or returned by this method is content containing text and HTML tags.
[Recommended learning: jQuery video tutorial, web front-end video]
The above is the detailed content of Can jquery clear text content?. For more information, please follow other related articles on the PHP Chinese website!