日期:2014-05-16 浏览次数:20589 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.test{background:green;}
</style>
<script>
//$("#nav ul li:has(ul)").addClass("has");
window.onload=function()
{
var aDiv=document.getElementById('nav');
var aUl=aDiv.getElementsByTagName('ul');
var aLi=null;
for(var i=0;i<aUl.length;i++)
{
aLi=aUl[i].getElementsByTagName('li');
for(var j=0;j<aLi.length;j++)
{
if(aLi[j].getElementsByTagName('ul')[0])
addClass(aLi[j],'test');
}
}
}
function addClass(obj,className)
{
obj.className=obj.className?obj.className+' '+className:className;
}
</script>
</head>
<body>
<div id="nav">
<ul>
<li>
<ul>
<li>测试1</li>
<li>测试2</li>
</ul>
</li>
<li>li2</li>
<li>
<ul>
<li>测试3</li>
<li>测试4</li>
</ul>
</li>
</ul>
</div>
</body>
</html>