简体   繁体   English

HTML使常规按钮的行为类似于一组单选按钮

[英]HTML make regular buttons act like a group of radio buttons

I want to get some "regular" buttons to act like a group of radio buttons. 我希望得到一些“常规”按钮,就像一组单选按钮一样。

I found a jQueryUI plugin that does exactly what I want: http://jqueryui.com/demos/button/#radio 我找到了一个jQueryUI插件,它完全符合我的要求: http//jqueryui.com/demos/button/#radio

But I'd like to avoid adding another library to my project. 但是我想避免在我的项目中添加另一个库。 Any idea how this could be done using pure HTML + javascript/jQuery ? 知道如何使用纯HTML + javascript / jQuery完成这项工作吗?

Thanks in advance 提前致谢

demo: http://jsfiddle.net/CXrgm/6 演示: http//jsfiddle.net/CXrgm/6

$('.button').click(function() {
   $('.button').removeClass('active');
   $(this).addClass('active');
});

where .button is your marker class for all buttons you need to include, and .active class whitch is indicate selected button. 其中.button是您需要包含的所有按钮的标记类,而.active.active表示选中的按钮。

to get the selected button use: 要使用所选按钮:

$('.button.active') // get attribute value, etc. Whatever you need.

If you want a plain js version, you can build your own small library like the following (let's call it min.js): 如果你想要一个简单的js版本,你可以构建自己的小型库,如下所示(让我们称之为min.js):

var util = util || {};

util.trim = function(s) {
  return s.replace(/(^\s+)|(\s+$)/g,'').replace(/\s+/g,' ');
}

util.dom = {};

util.dom.hasClassName = function(el, cName) {
    if (typeof el == 'string') el = document.getElementById(el);

    var re = new RegExp('(^|\\s+)' + cName + '(\\s+|$)');
    return el && re.test(el.className);
}

util.dom.addClassName = function(el, cName) {

    if (typeof el == 'string') el = document.getElementById(el);

    if (!util.dom.hasClassName(el, cName)) {
        el.className = util.trim(el.className + ' ' + cName);
    }
}

util.dom.removeClassName = function(el, cName) {

    if (typeof el == 'string') el = document.getElementById(el);

    if (util.dom.hasClassName(el, cName)) {
        var re = new RegExp('(^|\\s+)' + cName + '(\\s+|$)','g');
        el.className = util.trim(el.className.replace(re, ''));
    }
}


util.dom.getElementsByClassName = function(cName, root) {
  root = root || document.body;
  if (root.querySelectorAll) {
    return root.querySelectorAll('.' + cName);
  }

  var el, els = root.getElementsByTagName('*');
  var a = [];
  var re = new RegExp('(^|\\s)' + cName + '(\\s|$)'); 

  for (var i=0, iLen=els.length; i<iLen; i++) {
    el = els[i];

    if (el.className && re.test(el.className)) {
      a.push(el);
    }
  }
  return a;
}

and then do the button thing like: 然后做按钮之类的事情:

<style type="text/css">
.pressed {background-color: red}
</style>

<script type="text/javascript" src="min.js"></script>

<script type="text/javascript">
function buttonLike(el) {
  var els = util.dom.getElementsByClassName('radio');
  for (var i=0, iLen=els.length; i<iLen; i++) {
    util.dom.removeClassName(els[i], 'pressed');
  }
  util.dom.addClassName(el, 'pressed');
}
</script>

<button class="radio" onclick="buttonLike(this)">one</button>
<button class="radio" onclick="buttonLike(this)">two</button>
<button class="radio" onclick="buttonLike(this)">three</button>
<button class="radio" onclick="buttonLike(this)">four</button>
<button class="radio" onclick="buttonLike(this)">five</button>

The library code is just cut and paste from existing reusable code, the hand code part is only marginally more than that for various libraries. 库代码只是从现有的可重用代码中剪切和粘贴,手工代码部分仅略高于各种库的代码。 Needs a small addListener function, but that's only a few more lines in the library part. 需要一个小的addListener函数,但这只是库部分中的几行。

Event delegation could also be used so only one listener is required. 也可以使用事件委托,因此只需要一个监听器。

jQuery UI is the official user-interface plug-in for jQuery. jQuery UI是jQuery的官方用户界面插件。 You can safely add this to your project. 您可以安全地将其添加到您的项目中。 I'm sure it will be a perfect fit, and I'm sure you will find use for the various features elsewhere in your application. 我相信这将是一个完美的契合,我相信你会发现你的应用程序中的各种功能。

Both jQuery and jQuery UI are distributed across various Content Delivery Networks (CDNs), like Microsoft's or Google's , so you don't have to worry all that much about load-times or bandwidth costs. jQuery和jQuery UI都分布在各种内容交付网络(CDN)上,如微软谷歌 ,因此您不必担心加载时间或带宽成本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM