Javascript改变div的宽度onclick [英] Javascript change width of div onclick

查看:83
本文介绍了Javascript改变div的宽度onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着做三件事: onclick


  1. 有元素 id =notes_content更改 display:none to display:block
  2. 的元素含有 id =oct change width =1190px width =550px

  3. 包含 class =oct_days code> change width =168px to width =73px

完整代码的小提琴: http:// jsfiddle .net / ascottz / jX3wh /

前两个发生,但第三个不发生。我怀疑这是一个语法错误,但我自己无法理解。

解决方案

getElementsByClassName 返回一个dom元素数组,它不是单个实例。您必须遍历数组并对每个元素进行样式设置。



查看更新的小提琴。

  for(var i = 0; i  days [i] .style.width =73px; 
}

http://jsfiddle.net/jX3wh/4/


I'm trying to do three things onclick:

  1. have element with id="notes_content" change display:none to display: block
  2. have element with id="oct" change width = "1190px" to width = "550px"
  3. have elements with class="oct_days" change width = "168px" to width = "73px"

Fiddle of full code: http://jsfiddle.net/ascottz/jX3wh/

The first two happen, but the third does not. I suspect it is a syntax error, but can't catch it myself.

解决方案

getElementsByClassName returns an array of dom elements, it is not a single instance. You must loop over the array and style each element.

Have a look at the updated fiddle.

for( var i = 0; i < days.length; i++ ){
    days[i].style.width = "73px";        
}

http://jsfiddle.net/jX3wh/4/

这篇关于Javascript改变div的宽度onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆