简体   繁体   English

我如何 select 多个跨度元素?

[英]How can I select multiple span elements?

If I select my span element by ID it gets executed but I want to select multiple span elements.如果我按 ID select 我的 span 元素,它会被执行,但我想要 select 多个 span 元素。 I tried with class and name but it also not working.我尝试使用 class 和名称,但它也不起作用。

 const uname = document.getElementById('fname') const form = document.getElementById('form') const errorElement = document.getElementById('formerror') form.addEventListener('submit', (e) = \ > { let messages = \ [\] if (uname.value === '' || uname.value == null) { messages.push("Name is required") } if (messages.length\ > 0) { e.preventDefault() errorElement.innerHTML = messages.join(', ') } })
 <div id="error"> <form name="signupform" action="signupdetails.php" method="get" id="form"> <input type="text" class="text" placeholder="Username" id="fname"><b><span id="formerror"></span></b> <input type="text" class="text" placeholder="Roll:no" name="froll"><b><span id="formerror"></span></b> <input type="email" class="text" placeholder="Email" id="femail"><b><span id="formerror"></span></b> <i class="fa fa-eye-slash" aria-hidden="true" id="icon1"></i> <input type="password" class="text" placeholder="Password" id="password1" name="fpass"> <i class="fa fa-eye-slash" aria-hidden="true" id="icon2"></i> <input type="password" class="text" placeholder="Confirm Password" id="password2" name="fcpass"> <button id="btn" style="color: white;" type="submit">Sign up</button> <br> <br> <br> </form> </div>

An id should be unique in the entire document, see https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute一个id在整个文档中应该是唯一的,见https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute

Use a class on your span elements and query them with document.querySelectorAll('.formerror') .span元素上使用 class 并使用document.querySelectorAll('.formerror')查询它们。

 const uname = document.getElementById('fname') const form = document.getElementById('form') const errorElements = document.querySelectorAll('.formerror') console.log(errorElements)
 <div id="error"> <form name="signupform" action="signupdetails.php" method="get" id="form"> <input type="text" class="text" placeholder="Username" id="fname"><b><span class="formerror"></span></b> <input type="text" class="text" placeholder="Roll:no" name="froll"><b><span class="formerror"></span></b> <input type="email" class="text" placeholder="Email" id="femail"><b><span class="formerror"></span></b> <i class="fa fa-eye-slash" aria-hidden="true" id="icon1"></i> <input type="password" class="text" placeholder="Password" id="password1" name="fpass"> <i class="fa fa-eye-slash" aria-hidden="true" id="icon2"></i> <input type="password" class="text" placeholder="Confirm Password" id="password2" name="fcpass"> <button id="btn" style="color: white;" type="submit">Sign up</button> <br> <br> <br> </form> </div>

Just had a somewhat similar issue to this, & as others have already mentioned.刚刚遇到了与此类似的问题,正如其他人已经提到的那样。 You will want to change this snippet of code below in you're javascript您需要更改下面的这段代码,因为您是 javascript

document.getElementById('formerror')

To instead改为

document.querySelectorAll(".formerror") // Be sure to also change from and ID to instead using classes for the form error

Also as ID's are only meant to be used once, trying to assign them and use them more than once should not work as they are meant to be unique to one element.此外,由于 ID 只能使用一次,因此尝试分配它们并多次使用它们不应该起作用,因为它们对于一个元素来说是唯一的。 So this is why you should instead transition to adding them as classes instead.所以这就是为什么你应该改为将它们添加为类。

Now as for when you are trying to access you're span elements by Javascript, since there is more than one span element.现在,当您尝试访问 Javascript 的跨度元素时,因为有多个跨度元素。 You will need to ensure that you use the "querySelectorAll" function as this will allow you to target all of you're span elements.您需要确保使用“querySelectorAll”function,因为这将允许您定位所有跨度元素。 Otherwise if using just the "querySelector" it will only apply to the first span element, while the other two span elements remain un affected.否则,如果仅使用“querySelector”,它将仅应用于第一个 span 元素,而其他两个 span 元素不受影响。

Hope this could help to add a bit of insight and clear some things up for you.希望这可以帮助您增加一些洞察力并为您理清一些事情。

First change the id to a class attribute, because the id should be unique to the entire document.先把id改成一个class的属性,因为id对整个文档来说应该是唯一的。

<span class="formerror"></span>

Then Instead of using document.getElementById() , use document.querySelectorAll(".formerror") .然后不使用document.getElementById() ,而是使用document.querySelectorAll(".formerror")

It will solve your problem.它会解决你的问题。

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

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