Skip to content

Instantly share code, notes, and snippets.

@think49
Created September 26, 2010 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think49/597931 to your computer and use it in GitHub Desktop.
Save think49/597931 to your computer and use it in GitHub Desktop.
Yahoo知恵袋 (q1247699647)
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<title>getMapDivAttribute for chiebukuro (q1247699647)</title>
<style type="text/css"><!--
html { margin: 0; padding: 0; }
body { margin: 0; padding: 1em; }
pre { font-family: monospace; }
#PRINT { background-color: #ddd; }
--></style>
<script type="text/javascript"><!--
function print (doc, str) {
doc.getElementById('PRINT').firstChild.appendChild(doc.createTextNode(str + '\n'));
}
function getMapDivAttribute () {
var doc = document, spot, contents;
if (doc.querySelectorAll) contents = doc.querySelectorAll('#spotInfo>.contents');
else spot = doc.getElementById('spotInfo'), contents = spot.children || spot.childNodes;
for (var i=0,l=contents.length; i<l; i++) {
if ((' ' + contents[i].className + ' ').indexOf(' contents ') !== -1) {
for (var j=0, contents2=contents[i].children || contents[i].childNodes, m=contents2.length, element; j<m; j++) {
element = contents2[j];
if (element.tagName && element.tagName.toLowerCase() === 'h2') {
print (doc, 'element: ' + element);
print (doc, 'element.firstChild.nodeValue: ' + element.firstChild.nodeValue);
}
}
}
}
}
//--></script>
</head>
<body onload="getMapDivAttribute();">
<div id="spotInfo">
<div class="contents">
<h2>AAA</h2>
<div class="comments">BBB</div>
<div class="img"><img src="./img/001.jpg" /></div>
<div class="access">CCC</div>
<span class="pos" >1</span>
<span class="pos" >2</span>
</div>
<div class="contents">
<h2>DDD</h2>
<div class="comments">EEE</div>
<div class="img"><img src="./img/002.jpg" /></div>
<div class="access">FFF</div>
<span class="pos" >3</span>
<span class="pos" >4</span>
</div>
<div class="contents">
<h2>GGG</h2>
<div class="comments">HHH</div>
<div class="img"><img src="./img/001.jpg" /></div>
<div class="access">III</div>
<span class="pos" >5</span>
<span class="pos" >6</span>
</div>
</div>
<pre id="PRINT"><code></code></pre>
</body>
</html>
@think49
Copy link
Author

think49 commented Sep 28, 2010

JavaScriptで入れ子のDIV要素内の要素にアクセスするには? - Yahoo!知恵袋
http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1247699647

にポストしたコードです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment