Home > Web Front-end > JS Tutorial > body text

Implementation method of dynamically changing div attributes in JavaScript_javascript skills

WBOY
Release: 2016-05-16 15:49:30
Original
1355 people have browsed it

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>

Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!