用JQ实现 当发现class值等于cid值时,一个DIV自动加入另一个DIV 的ul li中去
**用JQ实现当发现class值等于cid值时,一个DIV自动加入另一个DIV的ulli中去**<divid="topmenu"class="0"cid="23"><ah...
**用JQ实现 当发现class值等于cid值时,一个DIV自动加入另一个DIV 的ul li中去**
<div id="topmenu" class="0" cid="23">
<a href='/ChannelID/23'>产品中心</a>
</div>
<div id="topmenu" class="23" cid="25">
<a href='/ChannelID/25'>智能设备</a>
</div>
<div id="topmenu" class="25" cid="57">
<a href='/ChannelID/57'>无人机</a>
</div>
<div id="topmenu" class="0" cid="13">
<a href='/ChannelID/13'>新闻频道</a>
</div>
<div id="topmenu" class="13" cid="15">
<a href='/ChannelID/15'>新闻一</a>
</div>
<div id="topmenu" class="15" cid="57">
<a href='/ChannelID/57'>新闻一(1)</a>
</div>
**<br/>想实现的形式如下<br/>**
<div id="topmenu" class="0" cid="23">
<a href='/ChannelID/23'>产品中心</a>
<ul><li>
<div id="topmenu" class="23" cid="25">
<a href='/ChannelID/25'>智能设备</a>
<ul><li>
<div id="topmenu" class="25" cid="57">
<a href='/ChannelID/57'>无人机</a>
</div>
</li></ul>
</div>
</li></ul>
</div>
<div id="topmenu" class="0" cid="13">
<a href='/ChannelID/13'>新闻频道</a>
<ul><li>
<div id="topmenu" class="13" cid="15">
<a href='/ChannelID/15'>新闻一</a>
<ul><li>
<div id="topmenu" class="15" cid="57">
<a href='/ChannelID/57'>新闻一(1)</a>
</div>
</li></ul>
</div>
</li></ul>
</div> 展开
<div id="topmenu" class="0" cid="23">
<a href='/ChannelID/23'>产品中心</a>
</div>
<div id="topmenu" class="23" cid="25">
<a href='/ChannelID/25'>智能设备</a>
</div>
<div id="topmenu" class="25" cid="57">
<a href='/ChannelID/57'>无人机</a>
</div>
<div id="topmenu" class="0" cid="13">
<a href='/ChannelID/13'>新闻频道</a>
</div>
<div id="topmenu" class="13" cid="15">
<a href='/ChannelID/15'>新闻一</a>
</div>
<div id="topmenu" class="15" cid="57">
<a href='/ChannelID/57'>新闻一(1)</a>
</div>
**<br/>想实现的形式如下<br/>**
<div id="topmenu" class="0" cid="23">
<a href='/ChannelID/23'>产品中心</a>
<ul><li>
<div id="topmenu" class="23" cid="25">
<a href='/ChannelID/25'>智能设备</a>
<ul><li>
<div id="topmenu" class="25" cid="57">
<a href='/ChannelID/57'>无人机</a>
</div>
</li></ul>
</div>
</li></ul>
</div>
<div id="topmenu" class="0" cid="13">
<a href='/ChannelID/13'>新闻频道</a>
<ul><li>
<div id="topmenu" class="13" cid="15">
<a href='/ChannelID/15'>新闻一</a>
<ul><li>
<div id="topmenu" class="15" cid="57">
<a href='/ChannelID/57'>新闻一(1)</a>
</div>
</li></ul>
</div>
</li></ul>
</div> 展开
展开全部
同一个html下,不能使用相同的id给不同dom对象,所以,我将topmenu作为class赋给了所有div,代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery实现</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<div id="d1">
<div id="topmenu" class="topmenu 0" cid="23">
<a href='/ChannelID/23'>产品中心</a>
</div>
<div id="topmenu" class="topmenu 23" cid="25">
<a href='/ChannelID/25'>智能设备</a>
</div>
<div id="topmenu" class="topmenu 25" cid="57">
<a href='/ChannelID/57'>无人机</a>
</div>
<div id="topmenu" class="topmenu 0" cid="13">
<a href='/ChannelID/13'>新闻频道</a>
</div>
<div id="topmenu" class="topmenu 13" cid="15">
<a href='/ChannelID/15'>新闻一</a>
</div>
<div id="topmenu" class="topmenu 15" cid="57">
<a href='/ChannelID/57'>新闻一(1)</a>
</div>
</div>
<input type="button" value="ronghe" />
<script type="text/javascript">
$('input').click(function(){
let topmenus = $('.topmenu');
topmenus.each(function(){
let the = $(this);
let cid = the.attr('cid');console.log(cid);
let targetDiv = $('.' + cid);console.log(targetDiv[0]);
if(targetDiv.length>0)
the.append($('<ul><li></li></ul>').append(targetDiv));
});
});
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery实现</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<div id="d1">
<div id="topmenu" class="topmenu 0" cid="23">
<a href='/ChannelID/23'>产品中心</a>
</div>
<div id="topmenu" class="topmenu 23" cid="25">
<a href='/ChannelID/25'>智能设备</a>
</div>
<div id="topmenu" class="topmenu 25" cid="57">
<a href='/ChannelID/57'>无人机</a>
</div>
<div id="topmenu" class="topmenu 0" cid="13">
<a href='/ChannelID/13'>新闻频道</a>
</div>
<div id="topmenu" class="topmenu 13" cid="15">
<a href='/ChannelID/15'>新闻一</a>
</div>
<div id="topmenu" class="topmenu 15" cid="57">
<a href='/ChannelID/57'>新闻一(1)</a>
</div>
</div>
<input type="button" value="ronghe" />
<script type="text/javascript">
$('input').click(function(){
let topmenus = $('.topmenu');
topmenus.each(function(){
let the = $(this);
let cid = the.attr('cid');console.log(cid);
let targetDiv = $('.' + cid);console.log(targetDiv[0]);
if(targetDiv.length>0)
the.append($('<ul><li></li></ul>').append(targetDiv));
});
});
</script>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询