javascript - 函数覆盖类而不是添加新的

标签 javascript html css

如何在不覆盖现有类的情况下向元素添加类?

我的函数捕获选中的单选按钮的值并将其添加到元素。

我遇到了麻烦,因为选中的单选按钮的值会覆盖现有类而不是添加第二个。

http://codepen.io/anon/pen/zGJXQj

document.addEventListener('DOMContentLoaded', function() {
  var radioButtons = document.getElementsByName('size');

  for(var i = 0; i < radioButtons.length; i++) {
    radioButtons[i].addEventListener('change', update, false);
  }

 function update() {
  var paragraph = document.querySelector('.element');
    paragraph.className = 'element';
    for(var i = 0; i < radioButtons.length; i++) {
      if (radioButtons[i].checked) {
        paragraph.classList.add(radioButtons[i].value);
      }
    }
  }

  update();
});


document.addEventListener('DOMContentLoaded', function() {
  var radioButtons = document.getElementsByName('color');

  for(var i = 0; i < radioButtons.length; i++) {
    radioButtons[i].addEventListener('change', update, false);
  }

 function update() {
  var paragraph = document.querySelector('.element');
    paragraph.className = 'element';
    for(var i = 0; i < radioButtons.length; i++) {
      if (radioButtons[i].checked) {
        paragraph.classList.add(radioButtons[i].value);
      }
    }
  }

  update();
});
.element{
width: 300px;
height: 100px;
margin-bottom: 50px;
border: 1px solid #111;
}

.small{
width: 300px;
height: 100px;
}

.big{
width: 400px;
height: 200px;
}

.blue{
background: #0000FF;
}

.green{
background: #00FF00;
}

.black{
background: #000;
}
<div class="element">
	</div> 

  
  
   
    <input type="radio" id="radio1" name="size" value="small" checked>
    <label for="radio1">option1</label>
    
    <input type="radio" id="radio2" name="size" value="big">
    <label for="radio2">option2</label>
  
  

<input id="blue" type="radio" name="color"  value="blue" checked/> 
<label for="blue">blue</label>

<input id="green" type="radio" name="color"  value="green"/> 
<label for="green">green</label>

<input id="black" type="radio" name="color"  value="black"/> 
<label for="black">black</label>

    

最佳答案

问题是您要创建两个事件处理程序,每个处理程序对应一个 radio 组。这会导致调用一个或另一个来单击单选按钮。其他按钮不参与!

解决方案:只创建一个事件处理程序。

document.addEventListener('DOMContentLoaded', function() {
  var radioButtons = Array.prototype.slice.call(document.getElementsByName('size')).concat(Array.prototype.slice.call(document.getElementsByName('color')));

  for (var i = 0; i < radioButtons.length; i++) {
    radioButtons[i].addEventListener('change', update, false);
  }

  function update() {
    var paragraph = document.querySelector('.element');
    paragraph.className = 'element';
    for (var i = 0; i < radioButtons.length; i++) {
      if (radioButtons[i].checked) {
        paragraph.classList.add(radioButtons[i].value);
      }
    }
  }

  update();
});
.element {
  width: 300px;
  height: 100px;
  margin-bottom: 50px;
  border: 1px solid #111;
}
.small {
  width: 300px;
  height: 100px;
}
.big {
  width: 400px;
  height: 200px;
}
.blue {
  background: #0000FF;
}
.green {
  background: #00FF00;
}
.black {
  background: #000;
}
<div class="element">
</div>


<input type="radio" id="radio1" name="size" value="small" checked>
<label for="radio1">option1</label>

<input type="radio" id="radio2" name="size" value="big">
<label for="radio2">option2</label>


<input id="blue" type="radio" name="color" value="blue" checked/>
<label for="blue">blue</label>

<input id="green" type="radio" name="color" value="green" />
<label for="green">green</label>

<input id="black" type="radio" name="color" value="black" />
<label for="black">black</label>

关于javascript - 函数覆盖类而不是添加新的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31582512/

相关文章:

javascript - 在 node.js 中产生一个子进程

javascript - 将 JQUERY 函数转换为原生 JavaScript

html - 将黑白视频转换为白色和一种颜色

html - 导致意外行为的媒体查询

javascript - Vue.js 当只有一个根节点时存在多个根节点

javascript - 让 jquery 计数器开始滚动

javascript - div弹出窗口不起作用

html - 在 Struts 中使用 HTML5 输入?

javascript - 使用innerHTML属性更新div的内容后,JS无法运行

阴影根或阴影根中所有顶级元素的 CSS 选择器