简体   繁体   English

"document.getElementById 不能选择多个元素"

[英]document.getElementById can't select more than one element

I'm working on loading.我正在加载。 I have div #loading<\/code> which is visible.我有 div #loading<\/code>这是可见的。 And more divs #message<\/code> which are hidden.还有更多隐藏的 div #message<\/code> 。 I have js function.我有js功能。

function loading() {
     setTimeout(function() {
         document.getElementById("loading").style.display = "none";
         document.getElementById("message").style.display = "block";
     }, 500, "fadeOut");
 }

You need to use unique ID's for your DOM elements.您需要为 DOM 元素使用唯一 ID。 Try modify your code like this:尝试像这样修改你的代码:

<script type="text/javascript">
function loading() {
  setTimeout(function() {
    document.getElementById("loading").style.display = "none";
    var el = document.getElementsByClassName('message');
    console.log(el);
    $.each(el, function(i, item){
    item.style.display = 'block';
    });
  }, 500, "fadeOut");
}
loading();
</script>
<style>
#loading {
  display: block;
}
.message{
  display: none;
}
</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="messages" onload="loading();">
  <div id="loading">
    ...
  </div>
  <div class="message">
    QWERTY
  </div>
  <div class="message">
    123456
  </div>
</div>

As the others have mentioned id s are unique and can only be used once on a page, so use a class instead.正如其他人提到的id是唯一的,并且只能在页面上使用一次,因此请改用类。 Here I've used querySelectorAll to grab a static list of classes.在这里,我使用querySelectorAll来获取类的静态列表。

The other issue is that you seem to be trying to use jQuery to fade the elements out, but you're not using jQuery for anything else.另一个问题是您似乎试图使用 jQuery 来淡出元素,但您没有将 jQuery 用于其他任何事情。 I'm going to suggest you CSS transitions.我将建议您使用 CSS 转换。 They're easy to use, and you don't have the need of loading a huge library.它们易于使用,您无需加载庞大的库。 Here I add new classes fadein and fadeout which (based on your code) increases/reduces the opacity of the specified elements to zero over three seconds.在这里,我添加了新类fadeinfadeout ,它们(根据您的代码)在三秒内将指定元素的不透明度增加/减少为零。

 function loading() { setTimeout(function() { // use a class for the loader too otherwise the transition // won't work properly const loader = document.querySelector('.loading'); loader.classList.add('fadeout'); // grab the elements with the message class const messages = document.querySelectorAll('.message'); // loop over them and add a fadeout class to each [...messages].forEach(message => message.classList.add('fadein')); }, 500); } loading();
 .loading { opacity: 1; } .message { opacity: 0; } .fadein { transition: opacity 3s ease-in-out; opacity: 1; } .fadeout { transition: opacity 3s ease-in-out; opacity: 0; }
 <div class="messages"> <div class="loading"> Loading </div> <div class="message"> QWERTY </div> <div class="message"> 123456 </div> </div>

ID attribute must be unique. ID属性必须是唯一的。 You can't use same ID multiple times on the page.您不能在页面上多次使用相同的 ID。 If you like to use same key then use it as a class or data-id which can be same or differ.如果您喜欢使用相同的键,则将其用作可以相同或不同的classdata-id

You cannot have same id twice in a document in order to select multiple elements group them by same Class rather than by id and then use the following to select them all.您不能在一个文档中两次使用相同的 id,以便选择多个元素,将它们按相同的类而不是按 id 分组,然后使用以下内容将它们全部选中。

document.querySelectorAll(".ClassName")

Or或者

document.getElementsByClassName(".ClassName");

Note that both methods returns a collection of all elements in the document with the specified class name, as a NodeList object.请注意,这两种方法都返回文档中具有指定类名的所有元素的集合,作为 NodeList 对象。

 function loading() { setTimeout(function() { document.getElementById("loading").style.display = "none"; document.getElementById("message").style.display = "block"; }, 500, "fadeOut"); } loading();<\/code><\/pre>
 #loading { display: block; } #message { display: none; }<\/code><\/pre>
 <script src="https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.1.1\/jquery.min.js"><\/script> <div class="messages" onload="loading();"> <div id="loading"> ... 
<div id="message"> QWERTY
<div id="message"> 123456 <\/code><\/pre>

"

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

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