1

このコードは機能します。しかし、それは私が望むものではありません。

<div>XPath example</div>
<script>
const result = document.evaluate('//div', document, null, 6, null);
console.log(result.snapshotItem(0));
</script>

外部からの文字列にXpathを使いたいのですが、以下のコードがNullです。

<script>
var string = "<div>XPath example</div>";
var doc = new DOMParser();
var htmlstring = doc.parseFromString(string, "text/html");
const result = htmlstring.evaluate('//div', document, null, 6, null);
console.log(result.snapshotItem(0));
</script>

外部から文字列を取得するためにJavascriptでXpathを使用するにはどうすればよいですか?

4

1 に答える 1