4 个回答

假设li里面是button,每个button都有id

$(button).on('click', function () {
  if ( $.data('checkedButton') ) { // 已经点击过,第二次点击时
    if ( $.data('checkedButton') !== this.id ) // 第二次点击的不是高亮按钮时
      reuturn false;
    } else { // 第二次点击的是高亮按钮时
      $.removeData('checkedButton');
      $(button).prop('disable', false);
    }
  } else { // 没有点击过,第一次点击时
    $.data('checkedButton', this.id);
    $(this).siblings().prop('disable', true);
  }
})

没用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)
    })
    
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题