0

每个人我都想在不使用 javascipt 中的 getelementsbyclassname 的情况下,在具有类“oshow”的 div 下获取所有具有类“a1”的 p 元素,这是在类“pro_line”下的 agian

 <div id="Zi03">
    <div class="ye">text</div>
    <div class="yee">text</div>
    <div class="pro-line">
         <div class="oshow">
         <div class="compic"><a href="#"></a></div>
         <p class="a1"><a href="poduct"></a></p>
         <p class="a2"><a href="poduct"></a></p>
         <div class="a3"><a href="poduct"></a></div>
        </div> 
         <div class="oshow">
         <div class="compic"><a href="#"></a></div>
         <p class="a1"><a href="poduct"></a></p>
         <p class="a2"><a href="poduct"></a></p>
         <div class="a3"><a href="poduct"></a></div>
        </div>
         <div class="oshow">
         <div class="compic"><a href="#"></a></div>
         <p class="a1"><a href="poduct"></a></p>
         <p class="a2"><a href="poduct"></a></p>
         <div class="a3"><a href="poduct"></a></div>
        </div> ...
        ....
        ....
    </div>
 </div>

请有人帮助我如何在不使用 getelementsbyclassname 的情况下在 javascript 中获取它,但我可以通过 ID 使用它

非常感谢提前....

4

3 回答 3

1

您可以使用以下替代getElementsByClassName()现代浏览器:

function getElementsByClassName(node, classname) {
        /// <summary>
        /// Replaces built-in function of modern browser. Implemented because not supported by IE7/8
        /// </summary>
        /// <param name="node">DOM element withing which search is done</param>
        /// <param name="classname">String with class name for search</param>
        /// <returns type="">Array of found elements</returns>

        var a = [];
        var re = new RegExp('(^| )' + classname + '( |$)');
        var els = node.getElementsByTagName("*");
        for (var i = 0, j = els.length; i < j; i++)
            if (re.test(els[i].className)) a.push(els[i]);
        return a;
    }

用类似这样的方式调用它:

var aElems = getElementsByClassName(document.getElementById('Zi03'),'pro-line')
于 2013-06-07T18:24:33.430 回答
0
var elem = document.getElementById('Zi03');
var proline = elem.children[2];

For loop for all immediate childs of proline 
   var oshow= proline.firstElementChild;
   var a1 = show.children[1]
   //display a1 Or whatever you want
End 

如果 a1 和其他元素处于同一层次结构级别,则此方法有效

于 2013-06-07T18:17:36.623 回答
0

这是一个可以帮助您入门的版本,它并不完美,但您可以改进它

var nodes = [];
var allChildNodes = document.getElementById('Zi03').getElementsByTagName('*');
for  (var i = 0; i < allChildNodes.length); i++ ) {
    var isA1 = ;
    var isUnderProline =  allChildNodes[i].parentNode.className == 'pro-line';
    var isUnderOShow = allChildNodes[i].parentNode.parentNode.className == 'oshow';

    if (
            allChildNodes[i].className == 'a1' &&
            allChildNodes[i].parentNode.className == 'pro-line' &&
            allChildNodes[i].parentNode.parentNode.className == 'oshow'  

       ){
       nodes.push(allChildNodes[i]);
    }
}
于 2013-06-07T18:31:31.087 回答