簡體   English   中英

有沒有一種標准的方法來在某個節點內獲取所有具有類名稱的元素?

[英]Is there a standard way to get all elements with a class name WITHIN a certain node?

我知道Document.getElementsByClassname(String)可以在整個文檔中獲取特定類名稱的所有元素。

我現在正在嘗試將那些元素放入文檔的特定節點中。 org.w3c.dom.Element ,我僅找到getElementsByTagname 我真的需要遍歷所有這些元素並讀取類名,還是有更好的方法?

我正在嘗試僅使用Java查找解決方案(可以與其他庫一起使用)。

非常感謝!

盡管您正在使用org.w3c.dom.Document對象,但這也應該起作用。

我在使用xalan從帶有Batik的SVG的SVG獲取XPath的特定元素中找到了解決方案。 唯一的區別是,由於與org.w3c.dom.xpath.XPathEvaluator實現的batik不一致,我最終使用了XPathAPI而不是XPathEvaluator

這是我編寫的代碼的相關部分:

Element searchRoot= document.getRootElement();
NodeList nl = XPathAPI.selectNodeList(searchRoot, ".//*[@class=\"class_name\"]");

jQuery是更好的方法。

這將在ID為“ nodeToSearchName”的節點中選擇所有類為“ classNameToFind”的節點。

$(“#nodeToSearchName”)。find(“。classNameToFind”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM