简体   繁体   English

JavaScript - 如何访问 event.currentTarget 的兄弟姐妹?

[英]JavaScript - How can I access the siblings of an event.currentTarget?

I created a basic voting system for a comment ratings bar.我为评论评分栏创建了一个基本的投票系统。 I'm trying to access the previous Sibling Element to update the votes but it's not working properly.我正在尝试访问以前的兄弟元素以更新投票,但它无法正常工作。 IAre you're supposed to use event.currentTarget or event.target?你应该使用 event.currentTarget 还是 event.target? Where did I go wrong?我在哪里 go 错了? Thank you.谢谢你。

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

HTML 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 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 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;
        } 
    }


}


Everything's working but inside the updateCommentVotes function, I should have been referencing the actual divs containing the textContent instead of the local variables (sign, number & percentage).一切正常,但在 updateCommentVotes function 中,我应该引用包含 textContent 而不是局部变量(符号、数字和百分比)的实际 div。

EDIT: It's a partial fix, I need each individual comment bar to refer to its own sign, number and percentage.编辑:这是一个部分修复,我需要每个单独的评论栏引用它自己的符号、数字和百分比。 It seems they all share the same number values.似乎它们都共享相同的数值。 Any tips are appreciated.任何提示表示赞赏。 Although, I believe its because I hard coded the values from siblings.虽然,我相信这是因为我对兄弟姐妹的值进行了硬编码。 Thank you.谢谢你。

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

JavaScript 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;
        
    }

}


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

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