【问题标题】:JavaScript - How can I access the siblings of an event.currentTarget?JavaScript - 如何访问 event.currentTarget 的兄弟姐妹?
【发布时间】:2023-03-24 01:05:02
【问题描述】:

我为评论评分栏创建了一个基本的投票系统。我正在尝试访问以前的兄弟元素以更新投票,但它无法正常工作。你应该使用 event.currentTarget 还是 event.target?我哪里做错了?谢谢。

https://jsfiddle.net/donfontaine12/bm9njcLt/46/#&togetherjs=qocecyJqyy

HTML

<div id="comment_ratings_bar">
  <div id="comment_rating_sign">+</div>
  <div id="comment_rating_num">0</div>
  <div id="comment_rating_percentage">[100.00%] </div>
  <div class="green_up_arrow"></div>
  <div class="red_down_arrow"></div>
</div>

<div id="comment_ratings_bar">
  <div id="comment_rating_sign">+</div>
  <div id="comment_rating_num">0</div>
  <div id="comment_rating_percentage">[100.00%] </div>
  <div class="green_up_arrow"></div>
  <div class="red_down_arrow"></div>
</div>

<div id="comment_ratings_bar">
  <div id="comment_rating_sign">+</div>
  <div id="comment_rating_num">0</div>
  <div id="comment_rating_percentage">[100.00%] </div>
  <div class="green_up_arrow"></div>
  <div class="red_down_arrow"></div>
</div>

<div id="comment_ratings_bar">
  <div id="comment_rating_sign">+</div>
  <div id="comment_rating_num">0</div>
  <div id="comment_rating_percentage">[100.00%] </div>
  <div class="green_up_arrow"></div>
  <div class="red_down_arrow"></div>
</div>

CSS

#comment_ratings_bar {
  width: 30%;
  margin: 0px 20px;
  padding: 0px 20px;
  font-size: 110%;
  font-weight: bolder;
  font-family: 'B612 Mono', monospace;
  color: lime;
  background-color: black;
  border: 0px solid black;
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.green_up_arrow {
  display: flex;
  flex-direction: row;
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 10px solid lime;
  cursor: pointer;
  margin: 0em 0.25em;
}

.red_down_arrow {
  display: flex;
  flex-direction: row;
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 10px solid red;
  cursor: pointer;
  margin: 0em 0.25em;
}


JavaScript

window.onload = function() {
  let commentUpvotes = 0;
  let commentDownvotes = 0;
  let totalCommentVotes = commentUpvotes + commentDownvotes;

  let commentRatingsBarAll = document.querySelectorAll("#comment_ratings_bar");

  for (let c of commentRatingsBarAll) {
    c.lastElementChild.previousElementSibling.addEventListener("click", updateCommentVotes);
    c.lastElementChild.addEventListener("click", updateCommentVotes);

  }
    function updateCommentVotes(e) {
        let siblings = getSiblings(e);
        let sign = siblings[0].textContent;
        let number = siblings[1].textContent;
        let percentage = siblings[2].textContent;

        if (sign && number && percentage) {
            let actualNumber = parseFloat(number.replace(/,/g, ''));

            if (e.target.className == "green_up_arrow") {
                actualNumber++; commentUpvotes++; totalCommentVotes++;
            } else {
                actualNumber--; commentDownvotes++; totalCommentVotes++;
            }

            if (actualNumber < 0) { sign.replace("+", ""); }
            
            percentage = "[" 
            + parseFloat((commentUpvotes / totalCommentVotes) * 100).toFixed(2) +"%]";

            number = actualNumber.toLocaleString();
        }
       
    }


    function getSiblings(element) {
        if (element) {
           let siblings = [];
        let sibling = element.parentNode.firstElementChild;

        while(sibling) {
            if (sibling.nodeType === 1 && sibling !== element) {
                siblings.push(sibling);
                sibling = sibling.nextElementSibling;
            }
        }
        return siblings;
        } 
    }


}


【问题讨论】:

    标签: javascript events target siblings


    【解决方案1】:

    一切正常,但在 updateCommentVotes 函数中,我应该引用包含 textContent 而不是局部变量(符号、数字和百分比)的实际 div。

    编辑:这是部分修复,我需要每个单独的评论栏引用自己的符号、数字和百分比。似乎它们都共享相同的数值。任何提示表示赞赏。虽然,我相信这是因为我对兄弟姐妹的值进行了硬编码。谢谢。

    在此处查看代码:https://jsfiddle.net/donfontaine12/bm9njcLt/46/#

    JavaScript

     window.onload = function() {
      let commentUpvotes = 0;
      let commentDownvotes = 0;
      let totalCommentVotes = commentUpvotes + commentDownvotes;
    
      let commentRatingsBarAll = document.querySelectorAll("#comment_ratings_bar");
    
      for (let c of commentRatingsBarAll) {
        c.lastElementChild.previousElementSibling.addEventListener("click", updateCommentVotes);
        c.lastElementChild.addEventListener("click", updateCommentVotes);
    
      }
    
       function updateCommentVotes(e) {
            let siblings = getSiblings(e);
            let sign = siblings[0].textContent;
            let number = siblings[1].textContent;
            let percentage = siblings[2].textContent;
    
            if (sign && number && percentage) {
                let actualNumber = parseFloat(number.replace(/,/g, ''));
    
                if (e.target.className == "green_up_arrow") {
                    actualNumber++; commentUpvotes++; totalCommentVotes++;
                } else {
                    actualNumber--; commentDownvotes++; totalCommentVotes++;
                }
    
                if (actualNumber < 0) { siblings[0].textContent.replace("+", ""); }
                
                siblings[2].textContent = "[" 
                + parseFloat((commentUpvotes / totalCommentVotes) * 100).toFixed(2) +"%]";
    
                siblings[1].textContent = actualNumber.toLocaleString();
    
            }
           
        }
    
        function getSiblings(element) {
            let siblings = [];
            let sibling = element.target.parentNode.firstElementChild;
    
            while(sibling) {
                if (sibling.nodeType === 1 && sibling !== element) {
                    siblings.push(sibling);
                    sibling = sibling.nextElementSibling;
                }
            }
            return siblings;
            
        }
    
    }
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-04
      • 2011-05-01
      • 1970-01-01
      • 2019-05-06
      • 2019-08-29
      • 1970-01-01
      • 2014-07-12
      相关资源
      最近更新 更多