Home >Web Front-end >JS Tutorial >jQuery innerText() vs outerText()
Core points
innerText()
and outerText()
are JavaScript properties that operate on the text content of HTML elements. innerText()
replaces the content between the element's start and end tags, while outerText()
removes the element and replaces it with the specified text. innerText()
and outerText()
can be used for any HTML element. However, using outerText()
on elements containing other elements will replace these elements together with text. These methods are supported in most modern browsers, but not in Internet Explorer 9 and earlier. innerText()
in outerText()
jQuery include the .text()
method (gets or sets the text content of an element and uses it with the jQuery collection) and the .html()
method (gets or sets the HTML content of an element, including tags). jQuery innerText()
Comparison with jQuery outerText()
jQuery's innerText()
and outerText()
functions are almost the same. Note that these can be called jQuery functions, but they are actually JavaScript properties. For ease of explanation, we will use jQuery! :) jQuery innerText()
attribute replaces the content between the element's start and end tags with the specified text, while the outerText()
attribute removes the element and replaces it with the specified text. What you need to remember is:
innerText
Replace only the content inside the tag outerText
Replace content and its tags themselvesjQuery innerText()
Function example
//简单的示例,用于更改页面元素的文本 document.getElementById("elementID").innerText = "我爱 jQuery4u 博客!";
<p id="elementID">这里将更改的文本。</p> <button onclick="changeText()">更改文本</button> <button onclick="resetText()">重置</button>
jQuery outerText()
Function example
document.getElementById("elementID").outerText = "我爱 jQuery4u 博客!";
<p id="elementID">是的,我 <3 jQuery!</p> <button id="myButton" onclick="myFunction()">点击我</button>
jQuery innerText
and outerText
FAQ (FAQ)
innerText
in jQuery? outerText
and innerText
are used to manipulate the text content of HTML elements. The main difference between them is how they handle HTML tags. outerText
Gets or sets the text between the start and end tags of an HTML element, excluding the tag itself. On the other hand, innerText
gets or sets text including HTML tags. This means that if you set text with outerText
, it will replace the entire element with the specified text, including the label. outerText
innerText
and outerText
in jQuery? To use innerText
or outerText
in jQuery, you need to select the HTML element you want to operate on and call the corresponding method. For example, to set the internal text of a paragraph element, you can use the following code:
$('p').innerText = '新文本';
To set external text, you can use:
$('p').outerText = '新文本';
Remember that setting external text will replace the entire element with the specified text, including the label.
innerText
and outerText
for any HTML element? Yes, you can use innerText
and outerText
for any HTML element. However, remember that these methods only affect the text content of the element. If an element contains other elements or attributes, it will not be affected.
outerText
? If you use outerText
to set the text of elements containing other elements, these elements will be replaced with the text. This is because outerText
replaces the entire element, including the label, with the specified text.
innerText
in outerText
? Yes, there are several alternatives to innerText
and outerText
in jQuery. For example, you can use the .text()
method to get or set the text content of an element. This method works similarly to innerText
, but it also works with jQuery collections, not just a single element. Another alternative is the .html()
method, which gets or sets the HTML content of the element, including the tags.
innerText
and outerText
in all browsers? innerText
and outerText
are supported in most modern browsers including Chrome, Firefox, Safari, and Edge. However, they are not supported in Internet Explorer 9 and earlier.
(The following questions and answers are covered in the original text, and duplication is omitted here)
The above is the detailed content of jQuery innerText() vs outerText(). For more information, please follow other related articles on the PHP Chinese website!