\r\n
\r\nfuga\r\npiyo\r\npiyo\r\npiyo\r\n
\r\n```","dateModified":"2018-04-19T05:03:53.359Z","datePublished":"2018-04-19T05:03:53.359Z","upvoteCount":0,"url":"https://teratail.com/questions/122496#reply-186637"},"suggestedAnswer":[{"@type":"Answer","text":"要素の子要素を全て取得する\r\n\r\n```lang=javascript\r\nconst elements = document.getElementById('list').children;\r\nfor (var i = 0; i < elements.length; i++) {\r\n console.log(elements[i]);\r\n};\r\n```","dateModified":"2018-04-18T14:54:16.459Z","datePublished":"2018-04-18T14:42:50.537Z","upvoteCount":0,"url":"https://teratail.com/questions/122496#reply-186499","comment":[]},{"@type":"Answer","text":"### Selectors API\r\n\r\n```JavaScript\r\ndocument.querySelectorAll('#a>.b').forEach(b => console.log(b));\r\n```\r\n\r\n### XPath\r\n\r\n`:scope` はブラウザの実装が整っていないので、コンテキストノード指定が必要であれば、`document.evaluate` をお勧めします。\r\n\r\n- [:scope - CSS: Cascading Style Sheets | MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/:scope)\r\n- [Introduction to using XPath in JavaScript | MDN](https://developer.mozilla.org/ja/docs/Introduction_to_using_XPath_in_JavaScript)\r\n\r\nRe: shim1 さん","dateModified":"2018-04-18T13:39:28.858Z","datePublished":"2018-04-18T13:39:28.858Z","upvoteCount":2,"url":"https://teratail.com/questions/122496#reply-186477","comment":[{"@type":"Comment","text":"お勉強目的なら、getElementById -> children -> for -> classList\r\nswitch は不要なような(classNameを使用しようとしてる?)","datePublished":"2018-04-18T13:45:49.602Z","dateModified":"2018-04-18T13:45:49.602Z"},{"@type":"Comment","text":"childNodes から辿るなら、nodeType + switch できますね。\r\n私的には tagName 判定しそうですが。","datePublished":"2018-04-18T13:58:55.488Z","dateModified":"2018-04-18T13:58:55.488Z"}]},{"@type":"Answer","text":"例えばこんな\r\n```HTML\r\n
\r\n\t
\r\n\t\t
\r\n\t
\r\n
\r\n```\r\n```JavaScript\r\n{\r\n\tconst a = document.getElementById(\"a\");\r\n\tif(a.querySelector(\":scope>div>div\")){//#bとかを指定できる\r\n\t\tkansuu();\r\n\t}\r\n\tfunction kansuu(){\r\n\t\tconsole.log(\"called\");\r\n\t}\r\n}\r\n```","dateModified":"2018-04-18T13:27:32.046Z","datePublished":"2018-04-18T13:12:50.808Z","upvoteCount":1,"url":"https://teratail.com/questions/122496#reply-186470","comment":[]}],"breadcrumb":{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://teratail.com","name":"トップ"}},{"@type":"ListItem","position":2,"item":{"@id":"https://teratail.com/tags/JavaScript","name":"JavaScriptに関する質問"}},{"@type":"ListItem","position":3,"item":{"@id":"https://teratail.com/questions/122496","name":"javascriptでid(class)を持っていたらある動きをさせたい"}}]}}}
質問するログイン新規登録

Q&A

解決済

4回答

383閲覧

javascriptでid(class)を持っていたらある動きをさせたい

shim1

総合スコア46

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2018/04/18 13:07

0

0

JavaScriptでid(class)を取得後、その子要素がある要素を持ってたらある動きをさせる、というようにしたいのですが方法が思いつきません。

例えばswitch文で

JavaScript

1var a = document.getElementById('a'); //要素を取得 2if(a.hasChildNodes()){ 3switch (???) { //aの子要素を比べたい 4 case b: //bというidをもっていたらある動きをさせたい 5 kansuu(); 6break; 7  8

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答4

0

Selectors API

JavaScript

1document.querySelectorAll('#a>.b').forEach(b => console.log(b));

XPath

:scope はブラウザの実装が整っていないので、コンテキストノード指定が必要であれば、document.evaluate をお勧めします。

Re: shim1 さん

投稿2018/04/18 13:39

think49

総合スコア18196

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

think49

2018/04/18 13:45

お勉強目的なら、getElementById -> children -> for -> classList switch は不要なような(classNameを使用しようとしてる?)
think49

2018/04/18 13:58

childNodes から辿るなら、nodeType + switch できますね。 私的には tagName 判定しそうですが。
guest

0

例えばこんな

HTML

1<div id="a"> 2 <div> 3 <div></div> 4 </div> 5</div>

JavaScript

1{ 2 const a = document.getElementById("a"); 3 if(a.querySelector(":scope>div>div")){//#bとかを指定できる 4 kansuu(); 5 } 6 function kansuu(){ 7 console.log("called"); 8 } 9}

投稿2018/04/18 13:12

編集2018/04/18 13:27
defghi1977

総合スコア4756

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

例えばこんな感じ

javascript

1<script> 2HTMLElement.prototype.has=function(selector){ 3 return this.querySelectorAll(selector).length>0?this:null; 4} 5window.addEventListener('DOMContentLoaded', function(e){ 6 var node=document.querySelector('#hoge').has('#fuga'); 7 if(node) console.log("#fuga ok"); 8 var node=document.querySelector('#hoge').has('#piyo'); 9 if(node) console.log("#piyo ok"); 10 var node=document.querySelector('#hoge').has('.piyo'); 11 if(node) console.log(".piyo ok"); 12 var node=document.querySelector('#hoge').has('span'); 13 if(node) console.log("span ok"); 14}); 15</script> 16<div id="hoge"> 17<span id="fuga">fuga</span> 18<span class="piyo">piyo</span> 19<span class="piyo">piyo</span> 20<span class="piyo">piyo</span> 21</div>

投稿2018/04/19 05:03

yambejp

総合スコア118164

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

think49

2018/04/19 11:59

> HTMLElement.prototype.has これは Element.prototype.matches() と同等でしょうか。
yambejp

2018/04/19 12:14 編集

やっていることはjQueryでいうhasのような処理です var node=$('#hoge').has('#fuga'); if(node.length>0) console.log("#fuga ok"); jQueryの場合hasがマッチしなくてもオブジェクトが得られるので $('#hoge').has('#fuga').css("background-Color","red"); のような処理が可能ですが prototypeで処理してると document.querySelector('#hoge').has('#fuga').style.backgroundColor="red"; のような処理をすると、#fugaをもっていないときnullに対して 処理をしようとしてエラーに成るので一度変数に受けて var node=document.querySelector('#hoge').has('#fuga'); if(node) node.style.backgroundColor="red"; のようにしなくてはいけません
think49

2018/04/19 12:52

なるほど、メソッドチェーンの為の拡張なんですね。
guest

0

要素の子要素を全て取得する

lang=javascript

1const elements = document.getElementById('list').children; 2for (var i = 0; i < elements.length; i++) { 3 console.log(elements[i]); 4};

投稿2018/04/18 14:42

編集2018/04/18 14:54
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.29%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問