orを使用してワイルドカード要素名の一致を行う方法はありますquerySelector
かquerySelectorAll
?
解析しようとしている XML ドキュメントは、基本的にプロパティのフラット リストです。
- 名前に特定の文字列が含まれる要素を見つける必要があります。
- 属性クエリではワイルドカードがサポートされていますが、要素自体ではサポートされていません。
明らかに非推奨の XPath (IE9 で削除された) の使用に戻る以外の解決策は受け入れられます。
orを使用してワイルドカード要素名の一致を行う方法はありますquerySelector
かquerySelectorAll
?
解析しようとしている XML ドキュメントは、基本的にプロパティのフラット リストです。
明らかに非推奨の XPath (IE9 で削除された) の使用に戻る以外の解決策は受け入れられます。
[id^='someId']
で始まるすべての ID に一致しsomeId
ます。
[id$='someId']
で終わるすべての ID に一致しsomeId
ます。
[id*='someId']
を含むすべての ID に一致しますsomeId
。
属性を探している場合は、name
に置き換えid
てname
ください。
要素のタグ名について話している場合、使用する方法があるとは思いませんquerySelector
例
Important_foo
コード
var nodeList = document.querySelectorAll('[aria-label^="Important_foo"]');
nodeList.forEach(node=> { console.log(node.getAttribute("aria-label"))});
tagNameを明示的な属性として設定します。
for(var i=0,els=document.querySelectorAll('*'); i<els.length;
els[i].setAttribute('tagName',els[i++].tagName) );
ネストされたタグがで終わるXMLドキュメントの場合、これが自分で必要でした_Sequence
。詳細については、 JaredMcAteerの回答を参照してください。
document.querySelectorAll('[tagName$="_Sequence"]')
私はそれがきれいだとは言いませんでした:)PS:tag_name
tagNameよりも使用することをお勧めします。そうすれば、「コンピューターで生成された」暗黙のDOM属性を読み取るときに干渉に遭遇することはありません。
この短いスクリプトを書きました。動作するようです。
/**
* Find all the elements with a tagName that matches.
* @param {RegExp} regEx regular expression to match against tagName
* @returns {Array} elements in the DOM that match
*/
function getAllTagMatches(regEx) {
return Array.prototype.slice.call(document.querySelectorAll('*')).filter(function (el) {
return el.tagName.match(regEx);
});
}
getAllTagMatches(/^di/i); // Returns an array of all elements that begin with "di", eg "div"
あるものではないという言い方があります。決してそうならないものにしないでください。優れた CSS セレクター リファレンス: https://www.w3schools.com/cssref/css_selectors.asp :not セレクターを次のように示します。
:not(selector) :not(p) Selects every element that is not a <p> element
次に例を示します: div の後に何か (az タグ以外のもの)
div > :not(z){
border:1px solid pink;
}