在jQuery中,text()和innerHTML之间有什么区别?

时间:2021-12-03 19:44:28

I have div elements and hold text/string inside them, then I try to iterate them, and text() doesn't work, but innerHTML work just fine.

我有div元素并在其中保存文本/字符串,然后我尝试迭代它们,text()不起作用,但innerHTML工作得很好。

var arr = $('div.elm');
$.each(arr, function(i,j){
    alert(j.text()); // it's not working

    console.log(j.text()); // nothing's in log

    alert(j.innerHTML); // works fine!
});

4 个解决方案

#1


4  

text() is a jQuery method, innerHTML is an DOM Element attribute.

text()是一个jQuery方法,innerHTML是一个DOM Element属性。

When you call $.each on a jQuery object, the second parameter you receive (the element) will be a DOM element, not a jQuery object.

当你在jQuery对象上调用$ .each时,你收到的第二个参数(元素)将是一个DOM元素,而不是一个jQuery对象。

  • The jQuery text() method is similar to calling innerText/textContent on a HTML Element.
  • jQuery text()方法类似于在HTML元素上调用innerText / textContent。

  • The jQuery html() method is similar to calling innerHTML on a HTML Element.
  • jQuery html()方法类似于在HTML元素上调用innerHTML。

If you want to use jQuery methods on your parameter passed in by each, you have to wrap it in a jQuery object:

如果要对每个传入的参数使用jQuery方法,则必须将其包装在jQuery对象中:

$.each(arr, function(i,j){
    alert($(j).text());
});

#2


2  

In your case, you should wrap the object in a jQuery object to use the text() method:

在您的情况下,您应该将对象包装在jQuery对象中以使用text()方法:

$.each(arr, function(i,j){
    alert($(j).text()); 

    console.log($(j).text()); 

    alert(j.innerHTML); 
});

innerHTML is an element attribute.

innerHTML是一个元素属性。

#3


1  

http://api.jquery.com/text/

Unlike the .html() method, .text() can be used in both XML and HTML documents. The result of the .text() method is a string containing the combined text of all matched elements. (Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.)

与.html()方法不同,.text()可以在XML和HTML文档中使用。 .text()方法的结果是一个包含所有匹配元素的组合文本的字符串。 (由于不同浏览器中HTML解析器的变化,返回的文本可能会在换行符和其他空格中有所不同。)

#4


1  

.text() returns JUST the text of that element and all of its descendant elements, where as .innerHTML returns all of the HTML in that element.

.text()返回该元素及其所有后代元素的文本,其中.innerHTML返回该元素中的所有HTML。

#1


4  

text() is a jQuery method, innerHTML is an DOM Element attribute.

text()是一个jQuery方法,innerHTML是一个DOM Element属性。

When you call $.each on a jQuery object, the second parameter you receive (the element) will be a DOM element, not a jQuery object.

当你在jQuery对象上调用$ .each时,你收到的第二个参数(元素)将是一个DOM元素,而不是一个jQuery对象。

  • The jQuery text() method is similar to calling innerText/textContent on a HTML Element.
  • jQuery text()方法类似于在HTML元素上调用innerText / textContent。

  • The jQuery html() method is similar to calling innerHTML on a HTML Element.
  • jQuery html()方法类似于在HTML元素上调用innerHTML。

If you want to use jQuery methods on your parameter passed in by each, you have to wrap it in a jQuery object:

如果要对每个传入的参数使用jQuery方法,则必须将其包装在jQuery对象中:

$.each(arr, function(i,j){
    alert($(j).text());
});

#2


2  

In your case, you should wrap the object in a jQuery object to use the text() method:

在您的情况下,您应该将对象包装在jQuery对象中以使用text()方法:

$.each(arr, function(i,j){
    alert($(j).text()); 

    console.log($(j).text()); 

    alert(j.innerHTML); 
});

innerHTML is an element attribute.

innerHTML是一个元素属性。

#3


1  

http://api.jquery.com/text/

Unlike the .html() method, .text() can be used in both XML and HTML documents. The result of the .text() method is a string containing the combined text of all matched elements. (Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.)

与.html()方法不同,.text()可以在XML和HTML文档中使用。 .text()方法的结果是一个包含所有匹配元素的组合文本的字符串。 (由于不同浏览器中HTML解析器的变化,返回的文本可能会在换行符和其他空格中有所不同。)

#4


1  

.text() returns JUST the text of that element and all of its descendant elements, where as .innerHTML returns all of the HTML in that element.

.text()返回该元素及其所有后代元素的文本,其中.innerHTML返回该元素中的所有HTML。

相关文章