简体   繁体   English

有什么方法可以获取与NodeJS中当前节点的标记名称不同的附近html节点?

[英]Any way to get nearby html nodes different from current node's tag name in NodeJS?

Let's say I have the following html code.. 假设我有以下html代码。

<html>
<head></head>
<body>  
  <div>
    <input type ="text">
    <img src = "pic.jpg">
    <a href ="index.html">Home</a>
  </div>
</body>
</html>

And I want to look at the nodes around an element that's on the same level. 我想看看同一级别的元素周围的节点。 For example, from the above html code, is it possible to do something like... 例如,从上面的html代码中,有可能做类似的事情...

 $("input").returnNodesOnSameLevel()

and it can return a list of nodes which here would contain [< input...>, < a...> ] ? 它可以返回一个节点列表,其中将包含[<input ...>,<a ...>]?

I'm using NodeJS's request module to grab the page source and then using Cheerio to do my parsing. 我正在使用NodeJS的请求模块来获取页面源,然后使用Cheerio进行解析。 The sibling() only returns nodes of the same tag name which makes sense. sibling()仅返回具有相同标签名称的节点,这是有意义的。 If there's a better module or another way to do this, please let me know. 如果有更好的模块或其他方法可以做到这一点,请告诉我。

Thank you. 谢谢。

如果$("input").siblings()不起作用,则可以尝试: $("input").parent().children()

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

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