简体   繁体   English

我如何从中获取元素<td> &lt;&gt;<td/> JavaScript DOM 中的(td 标签的子项)

[英]How Can I get element from <td><><td/> (td tag's child) in JavaScript DOM

td = tr[i].getElementsByTagName("td")[3];
console.log(td);

Here td displays below elements.这里 td 显示下面的元素。

                   <td style="display: none;">
                        <strong>Category:</strong> <span id="cate_name">Rice </span><br>
                        <strong>Product Code:</strong> 2132557596 <br>
                        <strong>Num of Sale:</strong> 0 Times <br>
                        <strong>Base Price:</strong> $65.00 <br>
                        <strong>Rating:</strong> 0 <br>
                    </td>

I need only <span id="cate_name">Rice </span><br> this area.我只需要<span id="cate_name">Rice </span><br>这个区域。 In details- I need only the fisrt line in How can I get it.详细地说 - 我只需要如何获得它中的第一行。

I try these but in console they show Uncaught TypeError: Cannot read properties of undefined (reading 'getElementsById' or tagname also)我尝试了这些,但在控制台中它们显示 Uncaught TypeError: Cannot read properties of undefined (reading 'getElementsById' or tagname also)

td.getElementsByID("cate_name")
td.getElementsByID("cate_name").value
td.getElementsByTagName("span")
td.getElementsTagName("span").value
and also use td[0].aboveThing

You need to use innerText , not value .您需要使用innerText ,而不是value


td.getElementById("cate_name").innerText
td.getElementsByTagName("span").innerText

Edit:编辑:

Sorry.对不起。 I have been "blinded" by the problem of the 'internal text', and I did not see any more.我一直被‘内文’的问题“蒙蔽”,再也看不到了。

The getElementById only works with document , because expects the element ids unique in the full document. getElementById仅适用于document ,因为期望元素 ID 在整个文档中是唯一的。

And the getElementsByTagName returns a node list. getElementsByTagName返回一个节点列表。

 const table = document.getElementById('id_table'); const trList = table.getElementsByTagName('tr'); // getElementById only works with document console.info(document.getElementById("cate_name").innerText) for (let i = 0; i < trList.length; i++) { const td = trList[i].getElementsByTagName("td")[0] console.info(td.getElementsByTagName("span")[0].innerText) }
 <table id="id_table"> <tr> <td> <strong>Category:</strong> <span id="cate_name">Rice </span><br> <strong>Product Code:</strong> 2132557596 <br> <strong>Num of Sale:</strong> 0 Times <br> <strong>Base Price:</strong> $65.00 <br> <strong>Rating:</strong> 0 <br> </td> </tr> </table>

The may be on the first 'line'...可能在第一条“线”...

However, the Document Object Model that HTML represents doesn't care about line breaks, or additional white space at all.但是,HTML 表示的文档对象模型根本不关心换行符或额外的空白。 Rather, it's about where in the tree it lies.相反,它是关于它在树中的位置。 The <span> is the second child to the <td>, (index of 1), but I don't know why it's the one you're trying to get to. <span> 是 <td> 的第二个孩子,(索引为 1),但我不知道为什么它是您想要到达的那个。 Is it the <span> tag itself?是 <span> 标签本身吗? or the second child element?还是第二个子元素?

If its the placement of the child within the td, you can retrieve all the children in an array:如果它是孩子在 td 中的位置,您可以检索数组中的所有孩子:

td.getElementsByTagName("*")

and then you can pull out the second child (again, index of [1]), and (optionally) get the content directly...然后你可以拉出第二个孩子(同样是 [1] 的索引),并且(可选)直接获取内容......

td.getElementsByTagName("*")[1].textContent

If it's the fact that it's a <span> that brings you there...如果事实上是 <span> 将您带到那里...

td.getElementsByTagName("span")[0].textContent

If you need to do it by the id="cate_name" attribute, the getElementById is available on the document node.如果您需要通过 id="cate_name" 属性执行此操作,则 getElementById 在文档节点上可用。 This is because properly formed HTML should not re-use an id:这是因为格式正确的 HTML 不应重复使用 id:

document.getElementById("cate_name").textContent

Also, this in the console of Chrome.此外,这在 Chrome 的控制台中。 Other browsers use slightly different JavaScript calls, such as innerText.其他浏览器使用稍微不同的 JavaScript 调用,例如innerText。 Does the page already jQuery imported?页面是否已经 jQuery 导入? If so, all the inconsistencies between browsers can be avoided.如果是这样,则可以避免浏览器之间的所有不一致。 You can check for it with...你可以用...

$.fn.jquery

This will get the jQuery version number.这将获得 jQuery 版本号。 This page, Stack Overflow, currently responds with '1.12.4'此页面 Stack Overflow 当前响应为“1.12.4”

With jQuery you can just use CSS-style selectors to find your node in the DOM:使用 jQuery,您可以使用 CSS 样式的选择器在 DOM 中找到您的节点:

$("tr td:nth-child(2) span#cate_name").innerText

.. or just: .. 要不就:

$("#cate_name").innerText

So I was mistaken, td.getElementsByTagName("*") (and the asterisk in the quotes is vital) doesn't return an array, but rather a specialized HtmlCollection object.所以我错了, td.getElementsByTagName("*") (引号中的星号很重要)不返回一个数组,而是一个专门的 HtmlCollection 对象。 We can convert it to a true js array, and manipulate it like this:我们可以将它转换为真正的 js 数组,并像这样操作它:

var arr = [].slice.call(td.getElementsByTagName("*"));
var br = arr.find(x => x.localName == "br");  // find first line break
var firstRow = arr.slice(0, arr.IndexOf(br)) // first 'row' as array segment
var firstRowText = firstRow.map(x => x.innerText).Join() //if you want the whole first row text 

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

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