将元素的内容打印到控制台

时间:2022-10-18 20:44:49

I have a feed that is outputting content dynamically to an element. I want to take the text from element A and output it to the console log.

我有一个动态输出内容的元素。我想从元素A获取文本并将其输出到控制台日志。

Example:

<div class="elementa">ID5667</div>

Console Output:

ID : ID5667

ID:ID5667

I've tried a few things, but I'm either getting undefined or the full HTML of that element.

我已经尝试了一些东西,但我要么是未定义的,要么是该元素的完整HTML。

5 个解决方案

#1


6  

I think below should work for you.

我认为以下应该适合你。

var result = document.getElementsByClassName("elementa")[0].innerHTML;

console.log(result);

For more reference : getElementByClassName

有关更多参考:getElementByClassName

#2


0  

Pure JavaScript:

console.log('ID : ' + document.getElementsByClassName('elementa')[0].innerHTML);

jQuery:

console.log('ID : ' + $('.elementa').text());

#3


0  

Or if you want to use jQuery as a tag indicates you can do that using .text():

或者如果你想使用jQuery作为标记表明你可以使用.text()来做到这一点:

console.log($('.elementa').text());

It is also possible to use .html() but the behavior will be different if the HTML tags are present inside that tag. Compare two documentations.

也可以使用.html()但如果HTML标记存在于该标记内,行为将会有所不同。比较两个文档。

#4


0  

Using jQuery:

  • The .html() method gives you the HTML contents (see doc page). Use it as follows:

    .html()方法为您提供HTML内容(请参阅doc页面)。使用方法如下:

    console.log("ID: " + $("div.elementa").html())
    
  • If you just want the text contents, use the .text() method (see doc page):

    如果您只想要文本内容,请使用.text()方法(请参阅doc页面):

    console.log("ID: " + $("div.elementa").text())
    

#5


0  

If you are looking for the content of multiple classes you can do this:

如果您要查找多个类的内容,可以执行以下操作:

var elements = document.getElementsByClassName("className");

for (i = 0; i < elements.length; i++) {
    console.log(elements[i].innerHTML);
}

#1


6  

I think below should work for you.

我认为以下应该适合你。

var result = document.getElementsByClassName("elementa")[0].innerHTML;

console.log(result);

For more reference : getElementByClassName

有关更多参考:getElementByClassName

#2


0  

Pure JavaScript:

console.log('ID : ' + document.getElementsByClassName('elementa')[0].innerHTML);

jQuery:

console.log('ID : ' + $('.elementa').text());

#3


0  

Or if you want to use jQuery as a tag indicates you can do that using .text():

或者如果你想使用jQuery作为标记表明你可以使用.text()来做到这一点:

console.log($('.elementa').text());

It is also possible to use .html() but the behavior will be different if the HTML tags are present inside that tag. Compare two documentations.

也可以使用.html()但如果HTML标记存在于该标记内,行为将会有所不同。比较两个文档。

#4


0  

Using jQuery:

  • The .html() method gives you the HTML contents (see doc page). Use it as follows:

    .html()方法为您提供HTML内容(请参阅doc页面)。使用方法如下:

    console.log("ID: " + $("div.elementa").html())
    
  • If you just want the text contents, use the .text() method (see doc page):

    如果您只想要文本内容,请使用.text()方法(请参阅doc页面):

    console.log("ID: " + $("div.elementa").text())
    

#5


0  

If you are looking for the content of multiple classes you can do this:

如果您要查找多个类的内容,可以执行以下操作:

var elements = document.getElementsByClassName("className");

for (i = 0; i < elements.length; i++) {
    console.log(elements[i].innerHTML);
}