给 div 加上Some Text1!
and Some Text2!
an id
or a class
。这样,您可以执行以下操作:
通过 ID:
<li id="a1">
<div id="text1"><p>Some Text1!</p></div>
<div id="text2"><a href="#"></a>Some Text2!</div>
<div><input id="a1b" type="button" onClick="fun()"/></div>
</li>
<script>
function fun()
{
var text1 = document.getElementById("text1");
var text2 = document.getElementById("text2");
//do stuff with text1 and text2. E.g.: alert them:
alert(text1.innerHTML);
alert(text1.innerHTML);
}
并通过类做到这一点:
<li id="a1">
<div class="identify"><p>Some Text1!</p></div>
<div class="identify"><a href="#"></a>Some Text2!</div>
<div><input id="a1b" type="button" onClick="fun2('identify')"/></div>
</li>
function fun2(matchClass)
{
var elems = document.getElementsByTagName('*'), i;
for (i in elems) {
if((' ' + elems[i].className + ' ').indexOf(' ' + matchClass + ' ')
> -1)
{
//do stuff with the elements. E.g.: Alert them:
alert(elems[i].innerHTML);
}
}
</script>
参考:
https://stackoverflow.com/a/3808886/866930