简体   繁体   English

select 如何使用js在同一个div中的所有跨度

[英]How can select all spans in the same div using js

I have code that can select only the first span in code when I use getElementsByTagName I get an error我的代码可以 select 只有代码中的第一个跨度,当我使用getElementsByTagName时出现错误

TypeError: Failed to execute 'insertBefore' on 'Node': parameter 2 is not of type 'Node'. TypeError:无法在“Node”上执行“insertBefore”:参数 2 不是“Node”类型。 at HTMLDivElement.在 HTMLDivElement。

now anyone can help me to select all spans in the card also I have a lot of card contain spans现在任何人都可以帮助我 select 卡中的所有跨度我也有很多卡包含跨度

const last = document.getElementById('last'); // define parent contain all cards

last.addEventListener("click", (event)=>      // add click event in parent
  {
  if(event.target.tagName === 'I')            // Write 'I' not 'i'tag name must be upper case
    {
    const icon = event.target;                       // define the target element
    const card = icon.parentNode;
    if(icon.getAttribute('value') === 'remove')      // get attribute from i element to remove target card
      {
      last.removeChild(card);                        // remove target card
      }
    else if(icon.getAttribute('value') === 'edit')   // get attribute from i element to edit target card
      {
      const span      = card.firstElementChild;
      const input     = document.createElement('input');  // create input text to carry span value and can edit it
      input.type      = 'text';                           // add type to input
      input.value     = span.textContent;                 // take the text from span to the input
      card.insertBefore(input, span);                     // change the place between input and span
      card.removeChild(span);                             // remove span and add input in the same place
      icon.className  = 'fa fa-check-circle';             // change icon from 'fa-edit' to 'fa-check-circle' to change icon shape
      icon.setAttribute('id','green-color');              // change ID from 'edit' to 'green-color' to make icon have green color when hover on it
      icon.setAttribute('value','');                      // remove value from icon to enter in the next 'if else'
      }
    else if(icon.getAttribute('value') === '')
      {
      const input      = card.firstElementChild;
      const span       = document.createElement('span'); // create span to carry the input value
      span.textContent = input.value;                    // take the text from input to the span
      card.insertBefore(span, input);                    // change the place between input and span
      card.removeChild(input);                           // remove span and add input in the same place
      icon.className   = 'fa fa-edit';                   // change icon from 'fa-check-circle' to 'fa fa-edit' to change icon shape
      icon.setAttribute('id','');                        // remove ID 'green-color' to make icon have blue color when hover on it
      icon.setAttribute('class','fa fa-edit edit');      // change ID from 'green-color' to 'edit' to make icon have green color when hover on it
      icon.setAttribute('value','edit');                 // Make value 'edit' to enter in first 'if else' condation
      }
    }
  })

<div class="row last" id="last">
  <h4 class="exam-header col-12">Schedule of exams dates</h4>
  <a class="exam-info teacher-link text-md-center col-8" id="teacher-link" href="#">
    <span class="subject subject-name">Computer Science</span>
    <span class="subject subject-date">2021/12/2</span>
    <span class="subject subject-time">9:00 AM</span>
    <span class="subject subject-duration">2h</span>
    <i class="fa fa-edit edit" value="edit"></i>
    <i class="fa fa-times remove" value="remove"></i>
  </a>
  <a class="exam-info teacher-link text-md-center col-8" id="teacher-link" href="#">
    <span class="subject subject-name">Computer Science</span>
    <span class="subject subject-date">2021/12/2</span>
    <span class="subject subject-time">9:00 AM</span>
    <span class="subject subject-duration">2h</span>
    <i class="fa fa-edit edit" value="edit"></i>
    <i class="fa fa-times remove" value="remove"></i>
  </a>
</div>

There's a better way to listen to events on multiple div/cards.有一种更好的方法可以监听多个 div/card 上的事件。 Get a reference to the element holding all the cards, listen to clicks on that.获取对包含所有卡片的元素的引用,听听点击。

const cardContainer = document.querySelector('div.card-list');
cardContainer.addEventListener("click", (event) => {
  const clickedElement = event.target;
  // put your event logic here - might need to verify the 'target' is the expected element.
});

Another trick to keep in mind is to use querySelectorAll to get multiple matches, then loop over them:要记住的另一个技巧是使用querySelectorAll来获取多个匹配项,然后遍历它们:

const allSpans = document.querySelectorAll('div span');

allSpans.forEach((span) => {
    // do stuff with the span 
})
  • if you wants to try it by getElementsByTagName or ClassName you can try the following如果您想通过getElementsByTagNameClassName尝试它,您可以尝试以下方法
var div = document.getElementsByTagName("div")[0]
var spans = div.getElementsByTagName("span");
for(let i = 0; i < spans.length; i++) {
  spans[i].innerText = 123;
}

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

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