如何用js或后台代码动态生成导航栏
2016-08-22
展开全部
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
li {
display: inline-block;
width: 100%;
height: 30px;
line-height: 30px;
}
li:active, li:focus, .li-active {
background: #dddddd;
color: red;
}
</style>
<script type="text/javascript">
onload = function () {
function removeActiveClass(node) {
node.className = '';
}
document.querySelector('ul[id=test]').onclick = function (e) {
Array.prototype.forEach.call(document.querySelectorAll('ul[id=test] > li'), removeActiveClass);
var target = e.target;
target.className = 'li-active';
}
}
</script>
</head>
<body>
<ul id="test">
<li>1111</li>
<li>2222</li>
<li>3333</li>
<li>4444</li>
</ul>
</body>
</html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
li {
display: inline-block;
width: 100%;
height: 30px;
line-height: 30px;
}
li:active, li:focus, .li-active {
background: #dddddd;
color: red;
}
</style>
<script type="text/javascript">
onload = function () {
function removeActiveClass(node) {
node.className = '';
}
document.querySelector('ul[id=test]').onclick = function (e) {
Array.prototype.forEach.call(document.querySelectorAll('ul[id=test] > li'), removeActiveClass);
var target = e.target;
target.className = 'li-active';
}
}
</script>
</head>
<body>
<ul id="test">
<li>1111</li>
<li>2222</li>
<li>3333</li>
<li>4444</li>
</ul>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询