简体   繁体   English

为什么在调用document.getElementsByClassName时我的函数返回未定义?

[英]Why does my function return undefined when calling document.getElementsByClassName?

Im trying to create a simple switch using css/JavaScript. 我正在尝试使用css / JavaScript创建一个简单的开关。 In the following you can see that im calling 在下面,您可以看到即时通讯正在调用

document.getElementsByClassName('onOffSwitch').style.animation = 'off';

jsBin jsBin

What other code is needed for js to understand im talking about the div named 'onOffSwitch'? js要了解我所说的名为“ onOffSwitch”的div,还需要什么其他代码? Was wondering why this current codeset doesn't work and how to fix it. 想知道为什么当前的代码集不起作用以及如何解决它。

getElementsByClassName() returns collection. getElementsByClassName()返回集合。 Specify index like the following. 如下指定index

document.getElementsByClassName('onOffSwitch')[0].style.animation = 'off';

Because .getElementsByClassName() returns a live node list (collection) of elements and you are trying to style the collection, rather than one element within the collection: 因为.getElementsByClassName()返回元素的活动节点列表 (集合),并且您试图设置集合的样式,而不是集合中的一个元素的样式:

// Change the animation style of the first element with the class "onOffSwitch"
document.getElementsByClassName('onOffSwitch')[0].style.animation = 'off';

Also, because .getElementsByClassName() returns a "live" node list (a list that is updated every time you use the variable assigned to the list), it can really hinder performance. 另外,由于.getElementsByClassName()返回一个“活动”节点列表(每次使用分配给该列表的变量时都会更新的列表),因此,它实际上会降低性能。 In most cases, a static node list is better and you can get that with: 在大多数情况下,静态节点列表会更好,您可以使用以下方法实现:

document.querySelectorAll(".onOffSwitch");

But, the same rules apply. 但是,同样的规则适用。 That's an entire collection. 那是整个收藏。 You'll need to access individual elements within the collection to work with their properties. 您需要访问集合中的各个元素以使用其属性。

Now, looking at your code, you don't really want a collection at all. 现在,查看您的代码,您实际上根本不需要集合。 You only have the one div class="onOffSwitch" , so you could just get that one element with: 您只有一个div class="onOffSwitch" ,因此您可以使用以下命令获得一个元素:

document.querySelector("div.onOffSwitch");

And then you could work with it directly. 然后,您可以直接使用它。

 let state = { switchG: 'On', bits: [0,1,0,1] }; // Get reference to the div var divSwitch = document.querySelector('div.onOffSwitch'); // Set up event handler divSwitch.addEventListener("click", flipSwitch); function flipSwitch() { if (state.switchG == 'On'){ state.switchG = 'Off'; console.log(state.switchG); divSwitch.style.animation = 'off 1s 1'; } else { state.switchG = 'On'; console.log(state.switchG); divSwitch.style.animation = 'on 1s 1'; } } 
 .onOffSwitch{ background-color: #4dc71f; border-radius: 5px; position: absolute; left: 40%; height: 20px; width: 55px; overflow: hidden; text-align: center; cursor:pointer; } @keyframes off { 0%{ background-color: green; } 100%{ background-color: red; } } @keyframes on { 0%{ background-color: red; } 100%{ background-color: green; } } 
 <div class = 'onOffSwitch'>On</div> 

暂无
暂无

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

相关问题 为什么不能使用“ !! document.getElementsByClassName &amp;&amp; function(){return document.getElementsByClassName(obj)}”简化代码 - Why can't use “ !!document.getElementsByClassName && function(){return document.getElementsByClassName(obj)}” for simplifing codes 为什么$(&#39;。classname&#39;)和document.getElementsByClassName(&#39;classname&#39;)返回不同的东西? - why does $('.classname') & document.getElementsByClassName('classname') return different things? document.getElementsByClassName返回未定义 - document.getElementsByClassName returns undefined document.getElementsByClassName(“…”)[0]返回未定义 - document.getElementsByClassName(“…”)[0] returning undefined amCharts-document.getElementsByClassName(…)[0]未定义 - amCharts - document.getElementsByClassName(…)[0] is undefined 为什么 document.getElementsByName 返回一个 NodeList 而 document.getElementsByClassName 返回一个 HTMLCollection? - Why does document.getElementsByName return a NodeList while document.getElementsByClassName returns an HTMLCollection? document.getElementsByClassName(...) 返回“...不是函数” - document.getElementsByClassName(...) returns "... is not a function" javascript document.getElementsbyClassName不是函数 - javascript document.getElementsbyClassName is not a function 为什么document.getElementsByClassName不断返回未定义的数组? - Why is document.getElementsByClassName constantly returning an undefined array? 为什么在使用document.getElementsByClassName(…).value时出现undefined? - Why do I get `undefined` when using `document.getElementsByClassName(…).value`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM