扫码关注官方订阅号
人生最曼妙的风景,竟是内心的淡定与从容!
假设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) })
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
假设li里面是button,每个button都有id
没用js写。
这就是一个数组循环选取的问题嘛: