给你一个简单的原生html5实现的例子吧。没有使用jquery。
<!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>