简体   繁体   English

Javascript更改div onclick的宽度

[英]Javascript change width of div onclick

I'm trying to do three things onclick : 我正在尝试在onclick上做三件事:

  1. have element with id="notes_content" change display:none to display: block 具有id="notes_content"元素将display:none更改为display: block
  2. have element with id="oct" change width = "1190px" to width = "550px" 具有id="oct"元素将width = "1190px"更改为width = "550px"
  3. have elements with class="oct_days" change width = "168px" to width = "73px" 具有class="oct_days"元素,将width = "168px" class="oct_days"更改为width = "73px"

Fiddle of full code: http://jsfiddle.net/ascottz/jX3wh/ 提琴完整代码: 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. getElementsByClassName返回一个dom元素数组,它不是单个实例。 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/ http://jsfiddle.net/jX3wh/4/

document.getElementsByClassName returns somewhat an array of elements, so you cannot simply refer to it as a DOM element in hope that it will work as you refer to each element of the collection (that is possible in jQuery, btw), so you have to use the foreach loop (doesn't matter how are you gonna achieve this -- via simple for() , or for(x in y) , or any other way). document.getElementsByClassName返回某种程度的元素数组,因此您不能简单地将其称为DOM元素,以希望它在您引用集合的每个元素时都可以工作(在jQuery,btw中是可能的),因此必须使用foreach循环(无论如何,都可以通过简单的for()for(x in y)或任何其他方式来实现。

I usually use Array.forEach function, but the specific type of an array returned by the document.getElementsByClassName does not have such function in prototype, so you have to use [].forEach.call(inWhat,function(what){}) syntax, or fallback to for(...) syntax. 我通常使用Array.forEach函数,但是document.getElementsByClassName返回的数组的特定类型在原型中没有这样的函数,因此您必须使用[].forEach.call(inWhat,function(what){})语法,或回退到for(...)语法。

Check out this: http://jsfiddle.net/jX3wh/1/ Dunno if it works. 看看这个: http : //jsfiddle.net/jX3wh/1/ Dunno,如果可行。

Also, what the f is this??? 另外,这是什么f?

<div onclick="javascript:showDiv();" class="oct_days">

I am really very surprised this works. 我真的很惊讶这项工作。 You should use onclick="showDiv()" instead, I think. 我认为您应该改用onclick="showDiv()"

Somebody, please, tell me how does it work! 请有人告诉我它是如何工作的!

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

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