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';
}