我有以下的html:// span [2]爲什麼不選擇文檔中的第二個跨度?
<!doctype HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<body>
<p>A <span>one</span></p>
<p>B <span>two</span></p>
<p>C <span>three</span></p>
<p>D <span>four</span></p>
</body>
</html>
運行的XPath //span[1]
獲得的第一個跨度。但是//span[2]
返回null:
input: document.evaluate("//span[1]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
output: <span>one</span>
input: document.evaluate("//span[2]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
output: null
爲什麼會發生這種情況?
至於修復,這在SQL Server XPath中工作,不確定關於JS XPath:`(// span [1])[2]` – RichardTheKiwi 2011-01-24 09:51:50
非常感謝。我已經接受了埃利安的答案,因爲它略微更清楚。這個例子雖然很有用。 – 2011-01-24 10:11:23