E4X按文本内容过滤?
我正在尝试使用 E4X 在 JavaScript 中过滤一些 XML,并且有一些特定的需求。 鉴于以下情况:
var xml = <body> <div> <p>This is some text that I have.</p> </div> </div>;
我想在文档中搜索以“这是一些文本”开头的段落。
目前,我可以通过以下内容来获取该段落:
xml..div(p.text().toString().indexOf("This is some text") === 0)
但是,“真正的”XML 要复杂得多(想想:常规网页)。 不保证有一个 div 直接作为问题中的段落的父级。 同一父元素中相关段落之前/之后可能还有其他段落。
有任何想法吗?
I'm trying to filter some XML in JavaScript using E4X and have some specific needs. Given the following:
var xml = <body> <div> <p>This is some text that I have.</p> </div> </div>;
I want to search the document for paragraphs starting with "This is some text".
Currently I can the following to get at the paragraph:
xml..div(p.text().toString().indexOf("This is some text") === 0)
However, the "real" XML is much more complex (think: a regular web page). There is no guarantee that there will be a div directly parenting the paragraph(s) in questions. There may be other paragraphs before/after the paragraph(s) in question within the same parent element.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用双点运算符搜索所有
任何嵌套级别的节点,然后过滤掉节点以仅包含以所需字符串开头的节点。 你已经很接近了,你只需要去掉
Use the double dot operator to search for all <p> nodes of any nesting leveling and then filter out the nodes to only include ones that start with your desired string. You were close, you just need to get rid of the <div> since, as you've stated, the <div> might not be present.: