0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【JQuery】メソッドは文字列に機能しないこと

Posted at

背景
HTML要素を文字列htmlStringとして取得し、querySelectorAllを使おうとすると、うまくいかなかった。
原因
querySelectorAll()などのメソッドはHTMLに対して機能するが、HTML文字列に対して直接使うことはできない。
HTML文字列に対してquerySelectorAllを使用したい場合は、まず以下のように文字列をDOMに変換する必要がある。

//HTML文字列をDOMに変換
const parser = new DOMParser();
const doc = parser.parseFromString(htmlString, 'text/html');

//変換したDOMに対してquerySelectorAllを使用、例えば「tr」
const trElements = doc.querySelectorAll('tr');

//filterなどを使用
const filteredTrs = [...trElements].filter(tr => /*条件*/);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?