Home  >  Article  >  Web Front-end  >  Implementation method of dynamically changing div attributes in JavaScript_javascript skills

Implementation method of dynamically changing div attributes in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:49:301354browse

The example in this article describes the implementation method of dynamically changing div attributes in JavaScript. Share it with everyone for your reference. The details are as follows:

Here you can dynamically change div attributes, styles, etc. through JS

<script type="text/javascript">
  var oBox = document.getElementById('box');
  var oDiv = document.getElementById('div1');
  var aInput = document.getElementsByTagName('input');
  var aAttr = ['width', 'height', 'backgroundColor', 'display', 'display'];
  var aValue = ['200px', '200px', 'red', 'none', 'block'];
  for(var len=aInput.length,i=0;i<len;i++){
    aInput[i].index = i; //索引
    aInput[i].onclick = function(){
      //重置按钮,cssText清空DIV属性
      if(this.index == aInput.length - 1)oDiv.style.cssText = "";
      //设置DIV属性
      property(oDiv, aAttr[this.index], aValue[this.index]);
    };
  }
  //控制DIV属性
  function property(obj, attr, value){
    obj.style[attr] = value;
  }
</script>

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn