首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

jquery 获取元素,该如何解决

2013-02-17 
jquery获取元素本帖最后由 liutao132 于 2013-01-18 16:39:34 编辑li classwhere_left_iin where_botto

jquery 获取元素
本帖最后由 liutao132 于 2013-01-18 16:39:34 编辑


<li class="where_left_iin where_bottom"><a class="where_bottom" href="javascript:void(0)" onclick="showRightContent(this)">Asia</a>
<ul class="where_left_in where_block">
<li><a href="javascript:void(0)" onclick="showRightContent1(this)">India </a></li>
<div class="where_xian">
</div>
</ul>
</li>


 function showRightContent(obj) {
//如何给给<a href="javascript:void(0)" onclick="showRightContent1(this)">India </a>添加一个className(xxx)
}


本人对js不熟,自己试了一下
 var slibs = obj.siblings("where_left_in where_block");
            if (slibs != undefined && slibs.length > 0) {
                var li = slibs[0].children;
                if (li != undefined && li.length > 0) {
                    var aa = li[0].children;
                    aa[0].className = "where_come_on";
                }
            }
这个在 谷歌和火狐下面可以 IE下有问题 jquery javascript
[解决办法]
 function getNextSibling(startBrother) {
            endBrother = startBrother.nextSibling;
            while (endBrother.nodeType != 1) {
                endBrother = endBrother.nextSibling;
            }
            return endBrother;
        }

        function getNextSibling1(obj) {
            if (obj.nextSibling.nodeType == 3) {
                sibling = obj.nextSibling.nextSibling; // Moz. Opera
            }
            else {
                sibling = obj.nextSibling; // IE
            }
            return sibling;
        }
        function getFirstChild(obj) {
            for (i = 0; i < obj.childNodes.length; i++) {


                if (obj.childNodes[i].nodeType == 1)
                    return obj.childNodes[i];
                else
                    continue;
            }
        }

        function showRightContent(obj) {//点击主地址时的发生
            var currentLi = jQuery(".where_come_on");
            for (var i = 0; i < currentLi.length; i++) {
                currentLi[i].className = "";
            }

            var ul = getNextSibling(obj);
            var li = getFirstChild(ul);
            var aa = getFirstChild(li);
            aa.className = 'where_come_on';
         }

热点排行