Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript跳转到父标记并获取下一个标记的innerHTML?_Javascript_Dom - Fatal编程技术网

如何使用javascript跳转到父标记并获取下一个标记的innerHTML?

如何使用javascript跳转到父标记并获取下一个标记的innerHTML?,javascript,dom,Javascript,Dom,我想在Total:的强标记旁边得到值450。我似乎无法确定这一点。我尝试使用parentNode等,但找不到正确的组合。将是炉排 这是其中的一部分- 在这里你可以检查我的解决方案。我刚刚用文本Total搜索了带有强标记的元素,然后使用nextSibling找到了下一个兄弟,这对我来说很有用 const tds=[…document.querySelectorAlltd.text right]; const selected=tds.findtd=>td.innerHTML==`Total:`;

我想在Total:的强标记旁边得到值450。我似乎无法确定这一点。我尝试使用parentNode等,但找不到正确的组合。将是炉排

这是其中的一部分-


在这里你可以检查我的解决方案。我刚刚用文本Total搜索了带有强标记的元素,然后使用nextSibling找到了下一个兄弟,这对我来说很有用

const tds=[…document.querySelectorAlltd.text right]; const selected=tds.findtd=>td.innerHTML==`Total:`; 设nextSibling=selected.nextSibling; 而nextSibling&&nextSibling.nodeType!==1 { nextSibling=nextSibling.nextSibling; } console.lognextSibling.innerText.substr1; 小计: £450.00 总数: £450.00 试试这个:

var aTags=document.getElementsByTagNamestrong; var searchText=总计:; var发现; 对于var i=0;i我相信您的代码有几个问题:

首先,出于某种原因,您需要将html包装在标记中,否则html无法正确解析

其次,search anchor Total:是其父节点的子节点,目标£450.00是该父节点的兄弟节点,而不是您在代码中使用的方式

因此,假设您的html是固定的,您需要更改

found = aTags[i];
console.log(found.previousNode);

其次,我认为使用xpath是实现目标的一种更干净的方法;大致如下:

target = document.evaluate( './/table//tr/td[strong[.="Total:"]]/following-sibling::td' ,document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

for (var i = 0; i < target.snapshotLength; i++) {
     price = target.snapshotItem (i);
     console.log(price.textContent);
};
found = aTags[i];
console.log(found.previousNode);
found = aTags[i];
console.log(found.parentNode.nextSibling.nextSibling.textContent);
target = document.evaluate( './/table//tr/td[strong[.="Total:"]]/following-sibling::td' ,document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

for (var i = 0; i < target.snapshotLength; i++) {
     price = target.snapshotItem (i);
     console.log(price.textContent);
};