1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <html>
<body>
<div id="div_toto">
<div id="div_id" class="div_class" name="div_name" bazooka="fatal">j'ai mis du texte dans ma div, c'est trop fun!</div>
</div>
<script>
function getElementByAttr(e,attr,value)
{
var tab = [];
if (e.getAttribute && e.getAttribute(attr)==value)
tab.push(e);
var n = e.firstChild;
if (n==null) return tab;
do
{
tab = tab.concat(getElementByAttr(n,attr,value));
}while((n = n.nextSibling)!=null)
return tab;
}
var toto_div = getElementByAttr(document,"id","div_toto")[0];
var msg = "texte de la div imbriquée :";
msg += "\n------- avec l'id -------\n" + getElementByAttr(toto_div,"id","div_id")[0].innerHTML;
msg += "\n------- avec la classe -------\n" + getElementByAttr(toto_div,"className","div_class")[0].innerHTML;
msg += "\n------- avec le nom -------\n" + getElementByAttr(toto_div,"name","div_name")[0].innerHTML;
msg += "\n------- avec le bazooka^^ -------\n" + getElementByAttr(toto_div,"bazooka","fatal")[0].innerHTML;
alert(msg);
</script>
</body>
</html> |
Partager