<ul>
<li><a href="#">1</a></li>
<li><a href="#">2</a>
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33</a>
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul></li>
</ul></li>
<li><a href="#">3</a></li>
</ul>
</div>
这样的菜单结构, 如何用JS判断下 如<a>标签后有ul 就在这个<a>里面添加<i></i>
变成这样
<div id="nav">
<ul>
<li><a href="#">1</a></li>
<li><a href="#">2 <i></i></a>
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33 <i></i></a>
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul></li>
</ul></li>
<li><a href="#">3</a></li>
</ul>
</div>
6 个解决方案
#1
第一个座位 来围观
#2
<!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>
<script>
window.onload=function()
{
var oDiv=document.getElementById('nav');
var aLink=oDiv.getElementsByTagName('a');
for(var i=0;i<aLink.length;i++)
{
var nextSibling=checkNext(aLink[i],'ul');
if(nextSibling)
{
var oI=document.createElement('i');
oI.innerHTML='123';//内容123
aLink[i].parentNode.insertBefore(oI,nextSibling);
}
}
}
function checkNext(obj,tag)
{
var nextSibling=getNext(obj);
if(nextSibling&&nextSibling.nodeName.toLowerCase()==tag)
return nextSibling;
return null;
}
function getNext(obj)
{
if(!obj||!obj.nextSibling) return null;
return obj.nextSibling.nodeType==1?obj.nextSibling:getNext(obj.nextSibling);
}
</script>
</head>
<body>
<div id="nav">
<ul>
<li><a href="#">1</a></li>
<li><a href="#">2</a>
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33</a>
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">3</a></li>
</ul>
</div>
</body>
</html>
#3
<!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=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function(){
var as=document.links;
for(var i=0;i<as.length;i++){
var n=as[i].nextSibling;
if(n&&!n.tagName){
n=n.nextSibling;
}
if(n&&n.tagName=='UL'){
as[i].innerHTML+='后面是ul';
}
}
}
</script>
</head>
<body>
<div id="nav">
<ul>
<li><a href="#">1</a></li>
<li><a href="#">2</a>
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33</a>
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">3</a></li>
</ul>
</div>
</body>
</html>
类似这样试试
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function(){
var as=document.links;
for(var i=0;i<as.length;i++){
var n=as[i].nextSibling;
if(n&&!n.tagName){
n=n.nextSibling;
}
if(n&&n.tagName=='UL'){
as[i].innerHTML+='后面是ul';
}
}
}
</script>
</head>
<body>
<div id="nav">
<ul>
<li><a href="#">1</a></li>
<li><a href="#">2</a>
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33</a>
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">3</a></li>
</ul>
</div>
</body>
</html>
类似这样试试
#4
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
#select {
border: 1px solid red;
}
#select li {
background-color: #2aa;
padding: 20px;
}
</style>
</head>
<body>
<div id="nav">
<ul>
<li><a href="#">1</a></li>
<li><a href="#">2</a>
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33</a>
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul></li>
</ul></li>
<li><a href="#">3</a></li>
</ul>
</div>
<script>
//找出所有a;
function findHref(){
var root = document.getElementById('nav');
var elems = [].slice.call(root.getElementsByTagName('a'));
return elems;
}
//过滤,剩下有后面有ul的a;
function filterHref(){
var elems = findHref();
var len = elems.length;
var f_elems=[];
for(var i=0;i<len;i++){
if(elems[i].nextElementSibling&&elems[i].nextElementSibling.nodeName=='UL'){
f_elems.push(elems[i]);
}
}
return f_elems;
}
//插入<i>
function insertI(){
var f_elems=filterHref();
var len=f_elems.length;
for(var i=0;i<len;i++){
var i_elem = document.createElement('i');
//i_elem.innerHTML='xx';
f_elems[i].appendChild(i_elem);
}
}
insertI();
</script>
</body>
</html>
#5
对这种替换,我还是喜欢转换为字符串进行操作.所以,这里给一个解题思路.仅供参考:
<div id="nav">
<ul>
<li><a href="#">1</a></li>
<li><a href="#">2</a>
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33</a>
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">3</a></li>
</ul>
</div>
<script type="text/javascript">
var nav = document.getElementById('nav');//获取对象
//重写nav
nav.innerHTML = nav.innerHTML //提取nav内部的html代码
.replace(/(\n|\r\n)/g,'') //先将换行替换掉
.replace(/<\/a\>(\s)*<ul>/gi,'<i></i></a><ul>');//对符合规则的标记做简单替换
//alert(nav.innerHTML);
</script>
#6
#1
第一个座位 来围观
#2
<!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>
<script>
window.onload=function()
{
var oDiv=document.getElementById('nav');
var aLink=oDiv.getElementsByTagName('a');
for(var i=0;i<aLink.length;i++)
{
var nextSibling=checkNext(aLink[i],'ul');
if(nextSibling)
{
var oI=document.createElement('i');
oI.innerHTML='123';//内容123
aLink[i].parentNode.insertBefore(oI,nextSibling);
}
}
}
function checkNext(obj,tag)
{
var nextSibling=getNext(obj);
if(nextSibling&&nextSibling.nodeName.toLowerCase()==tag)
return nextSibling;
return null;
}
function getNext(obj)
{
if(!obj||!obj.nextSibling) return null;
return obj.nextSibling.nodeType==1?obj.nextSibling:getNext(obj.nextSibling);
}
</script>
</head>
<body>
<div id="nav">
<ul>
<li><a href="#">1</a></li>
<li><a href="#">2</a>
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33</a>
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">3</a></li>
</ul>
</div>
</body>
</html>
#3
<!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=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function(){
var as=document.links;
for(var i=0;i<as.length;i++){
var n=as[i].nextSibling;
if(n&&!n.tagName){
n=n.nextSibling;
}
if(n&&n.tagName=='UL'){
as[i].innerHTML+='后面是ul';
}
}
}
</script>
</head>
<body>
<div id="nav">
<ul>
<li><a href="#">1</a></li>
<li><a href="#">2</a>
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33</a>
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">3</a></li>
</ul>
</div>
</body>
</html>
类似这样试试
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function(){
var as=document.links;
for(var i=0;i<as.length;i++){
var n=as[i].nextSibling;
if(n&&!n.tagName){
n=n.nextSibling;
}
if(n&&n.tagName=='UL'){
as[i].innerHTML+='后面是ul';
}
}
}
</script>
</head>
<body>
<div id="nav">
<ul>
<li><a href="#">1</a></li>
<li><a href="#">2</a>
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33</a>
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">3</a></li>
</ul>
</div>
</body>
</html>
类似这样试试
#4
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
#select {
border: 1px solid red;
}
#select li {
background-color: #2aa;
padding: 20px;
}
</style>
</head>
<body>
<div id="nav">
<ul>
<li><a href="#">1</a></li>
<li><a href="#">2</a>
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33</a>
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul></li>
</ul></li>
<li><a href="#">3</a></li>
</ul>
</div>
<script>
//找出所有a;
function findHref(){
var root = document.getElementById('nav');
var elems = [].slice.call(root.getElementsByTagName('a'));
return elems;
}
//过滤,剩下有后面有ul的a;
function filterHref(){
var elems = findHref();
var len = elems.length;
var f_elems=[];
for(var i=0;i<len;i++){
if(elems[i].nextElementSibling&&elems[i].nextElementSibling.nodeName=='UL'){
f_elems.push(elems[i]);
}
}
return f_elems;
}
//插入<i>
function insertI(){
var f_elems=filterHref();
var len=f_elems.length;
for(var i=0;i<len;i++){
var i_elem = document.createElement('i');
//i_elem.innerHTML='xx';
f_elems[i].appendChild(i_elem);
}
}
insertI();
</script>
</body>
</html>
#5
对这种替换,我还是喜欢转换为字符串进行操作.所以,这里给一个解题思路.仅供参考:
<div id="nav">
<ul>
<li><a href="#">1</a></li>
<li><a href="#">2</a>
<ul>
<li><a href="#">11</a></li>
<li><a href="#">22</a></li>
<li><a href="#">33</a>
<ul>
<li><a href="#">111</a></li>
<li><a href="#">222</a></li>
<li><a href="#">333</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">3</a></li>
</ul>
</div>
<script type="text/javascript">
var nav = document.getElementById('nav');//获取对象
//重写nav
nav.innerHTML = nav.innerHTML //提取nav内部的html代码
.replace(/(\n|\r\n)/g,'') //先将换行替换掉
.replace(/<\/a\>(\s)*<ul>/gi,'<i></i></a><ul>');//对符合规则的标记做简单替换
//alert(nav.innerHTML);
</script>