简体   繁体   English

使用Javascript检索文本时遇到问题

[英]Having trouble retrieving text using Javascript

I'm having trouble retrieving the text of an HTML element. 我在检索HTML元素的文本时遇到麻烦。

 var text_element = document.getElementById("details").getElementsByTagName("h3"); var text = text_element.innerText; console.log(text_element); console.log(text); 
 <div id="details"> <h3>Summary for</h3> </div> 

When I make the console call for text_element , I can actually see the h3 object info, but when I call text I get undefined . 当我在控制台中调用text_element ,实际上可以看到h3对象信息,但是当我调用text我会得到undefined

If I change the JS to be var text = text_element.innerHTML; 如果我将JS更改为var text = text_element.innerHTML; I get the same, undefined . 我得到相同的undefined

Shouldn't I be getting the text inside the h3 element, Summary for instead of undefined ? 我是否应该在h3元素( 摘要)中获取文本,而不是undefined

Can anyone tell me what I'm doing wrong and what I need to do to fix it? 谁能告诉我我做错了什么以及我需要怎么做才能解决?

I am not using jQuery, this is strictly pure Javascript. 我没有使用jQuery,这完全是纯Javascript。

The method .getElementsByTagName("h3") returns a NodeList (Or an HTMLCollection, now, in Firefox), not an Element, and NodeList prototype does not have a innerText property. 方法.getElementsByTagName("h3")返回一个NodeList(或Firefox中的HTMLCollection),而不是Element,并且NodeList原型不具有innerText属性。

You can get the first element of the node list with its index: 您可以获取节点列表的第一个元素及其索引:

 var text_elements = document.getElementById("details").getElementsByTagName("h3"); var text = text_elements[0].innerText; console.log(text_elements[0]); console.log(text); 
 <div id="details"> <h3>Summary for</h3> </div> 

Beware: NodeList and HTMLCollection objects are not Array s, even though they are array-like objects (like arguments ). 注意:NodeList和HTMLCollection对象不是Array ,即使它们是类似数组的对象(例如arguments )。 You can read more about Array-like objects . 您可以阅读有关数组类对象的更多信息

Here you are, faster: 您来了,更快:

 alert(document.querySelector('h3').innerText); 
 <div id="details"> <h3>Summary for</h3> </div> 

Hope this helps. 希望这可以帮助。

Approach:1 Accessing by id 方法:1通过id访问

var text_element = document.getElementById("details");
var text = text_element.innerText;
console.log(text_element);
console.log(text);

Approach:2 Accessing by getElementsByTagName("h3") . 方法:2通过getElementsByTagName("h3")访问。

var text_elements = document.getElementById("details").getElementsByTagName("h3");
var text = text_elements[0].innerText;
console.log(text_elements[0]);
console.log(text);

MDN - Element.getElementsByTagName MDN-Element.getElementsByTagName

The Element.getElementsByTagName() method returns a live HTMLCollection of elements with the given tag name. Element.getElementsByTagName()方法返回具有给定标签名称的元素的实时HTMLCollection。 The subtree underneath the specified element is searched, excluding the element itself. 搜索指定元素下面的子树,不包括元素本身。 The returned list is live, meaning that it updates itself with the DOM tree automatically. 返回的列表是活动的,这意味着它将自动使用DOM树进行更新。 Consequently, there is no need to call several times Element.getElementsByTagName() with the same element and arguments. 因此,无需使用相同的元素和参数多次调用Element.getElementsByTagName()。

When you get a text/content of tag by this method "getElementsByTagName()" - then it returns a collection of an elements's child elements with the specified tag name or can say collection of HTML and you can get by giving index. 当通过此方法“ getElementsByTagName()”获得标签的文本/内容时,它会返回具有指定标签名称的元素子元素的集合,或者可以说HTML的集合,您可以通过提供索引来获取。

Example. 例。

document.getElementsByTagName("h3"); - [<h3> Summary For </h3>]
document.getElementsByTagName("h3")[0]; - [<h3> Summary For </h3>]
document.getElementsByTagName("h3")[0].innerHTML; - Summary For

In your example 在你的例子中

var text_element = document.getElementById("details").getElementsByTagName("h3")[0];
var text = text_element.innerHTML;    
alert(text); // Summary For

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM