没用js写。
<style type="text/css">
input{
display: none;
}
label{
border: 1px solid;
display: inline-block;
height : 50px;
width : 100px;
font: 30px/50px "微软雅黑";
text-align:center;
}
input:checked+label{
background-color: aquamarine;
}
</style>
</head>
<body>
<input type="radio" name="country" id="usa" value="" />
<label for="usa">美国</label>
<input type="radio" name="country" id="china" value="" />
<label for="china">中国</label>
<input type="radio" name="country" id="spa" value="" />
<label for="spa">西班牙</label>
<input type="radio" name="country" id="other" value="" />
<label for="other">其他</label>
</body>
这就是一个数组循环选取的问题嘛:
var li=document.getElementsByTageName("li");//获取所有选项
for(var i=0,len=li.length;i<len;i++){//循环
(function(i){
li[i].onclick=function(){
//dosomthing;
alert(i);
}
})(i);
}
//HTML:
<ul id='select_ul'>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
//JS:
var lis = Array.prototype.slice.call(document.getElementById('select_ul').getElementsByTagName('li'));
var len = lis.length;
function handle() {
var activeLen = document.querySelectorAll('.active').length;
if (this.classList.contains('active')) {
this.classList.remove('active')
} else {
if (!activeLen) {
this.classList.add('active')
}else{
lis.forEach(function(item){
item.classList.remove('active')
})
this.classList.add('active')
}
}
}
lis.forEach(function(item, i) {
item.addEventListener('click', handle, false)
})
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
3 回答1.4k 阅读✓ 已解决
3 回答2.3k 阅读✓ 已解决
假设li里面是button,每个button都有id