回答編集履歴

1

コードの微修正

2021/08/07 16:52

投稿

think49
think49

スコア18166

test CHANGED
@@ -1,4 +1,4 @@
1
- `XPathResult` インターフェースのオブジェクトからノードを参照するには、`snapshotItem()` を使用する必要があります。
1
+ `XPathResult` インターフェースのオブジェクトからノードを参照するには、`XPathResult#snapshotItem()` を使用する必要があります。
2
2
 
3
3
 
4
4
 
@@ -8,15 +8,11 @@
8
8
 
9
9
  ```JavaScript
10
10
 
11
- var string = "<html><h1>aaa</h1></html>";
11
+ const htmlString = '<html><h1>aaa</h1></html>';
12
12
 
13
- var doc = new DOMParser();
13
+ const doc = new DOMParser().parseFromString(htmlString, 'text/html');
14
14
 
15
- var str = doc.parseFromString(string, "text/html");
16
-
17
- //evalateでxpathを使う。上手くいかない。
18
-
19
- var result = str.evaluate('//h1',document,null,6,null);
15
+ const result = doc.evaluate('//h1', document, null, 6, null);
20
16
 
21
17
  console.log(result.snapshotItem(0));
22
18