简体   繁体   English

如何确定单个元素的位置?

[英]How can I identify the position of a single element?

I'm creating a simple rating application, however, instead of all the circles turning green, I only want the selected circle to remain green. 我正在创建一个简单的评分应用程序,但是,不是让所有的圆圈变成绿色,而是让所选的圆圈保持绿色。

$('#rating-container').click(function () {
    var element = $('#target');
    var container = $('#rating-container');
    var index = container.children().get(element);
    var foundElement = container.children().get(index);
    var jQueryObject = $(foundElement);
    jQueryObject.addClass('rating-chosen');
});

The HTML: HTML:

<div id="rating-container">
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
  <div id="target" class="rating-circle"></div>
  <div class="rating-circle"></div>
  <div class="rating-circle"></div>
</div>

Codepen link below: 下面的Codepen链接:

https://codepen.io/CasaDeOrellana/pen/JqqJxZ https://codepen.io/CasaDeOrellana/pen/JqqJxZ

You can use .removeClass() with .toggleClass() like below. 您可以将.removeClass().toggleClass()如下所示。 And you can find the index using the .index() method. 您可以使用.index()方法找到索引。

 $("#rating-container div").click(function(index, item) { $('#rating-container div').removeClass('rating-chosen'); // Comment (Delete) this line if you don't want to remove class from previous selection $(this).toggleClass('rating-chosen'); console.log($(this).index()); }); 
 body { font-family: Verdana; } h1, h2, h3 { color: darkblue; } .rating-circle { height: 2em; width: 2em; border: 0.1em solid black; border-radius: 1.1em; display: inline-block; margin: 0; padding: 0.1em; } .rating-hover { background-color: yellow; } .rating-chosen { background-color: green; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="rating-container"> <div class="rating-circle"></div> <div class="rating-circle"></div> <div id="target" class="rating-circle"></div> <div class="rating-circle"></div> <div class="rating-circle"></div> </div> 

You should add the click event to the rating-circle element. 您应该将click事件添加到rating-circle元素中。

If you still need the index of the clicking child, here's the approach. 如果您仍然需要点击子项的索引,请采用以下方法。

As @Nevin mentioned, there's an easier way to get the index using jQuery 正如@Nevin所提到的,有一种使用jQuery来获取索引的简便方法

 $(document).ready(function() { $(".rating-circle").click(function() { $(this).addClass("rating-chosen"); // let index = [...this.parentElement.children].indexOf(this); let index = $(this).index() console.log(index); }); }); 
 body { font-family: Verdana; } h1, h2, h3 { color: darkblue; } .rating-circle { height: 2em; width: 2em; border: 0.1em solid black; border-radius: 1.1em; display: inline-block; margin: 0; padding: 0.1em; } .rating-hover { background-color: yellow; } .rating-chosen { background-color: green; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="rating-container"> <div class="rating-circle"></div> <div class="rating-circle"></div> <div class="rating-circle"></div> <div class="rating-circle"></div> <div class="rating-circle"></div> </div> 

Check this out. 看一下这个。 I just used a slightly different approach: 我只是使用了一种稍微不同的方法:

 $(function() { $(".rating-circle").click( (e) => { event = e || window.event; var target = event.target || event.srcElement; $(".rating-circle").removeClass("rating-chosen"); $("#" + target.id).addClass("rating-chosen"); } ) }); 
 body { font-family: Verdana; } h1, h2, h3 { color: darkblue; } .rating-circle { height: 2em; width: 2em; border: 0.1em solid black; border-radius: 1.1em; display: inline-block; margin: 0; padding: 0.1em; } .rating-hover { background-color: yellow; } .rating-chosen { background-color: green; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="rating-container"> <div class="rating-circle _1" id="1"></div> <div class="rating-circle _2" id="2"></div> <div class="rating-circle _3" id="3"></div> <div class="rating-circle _4" id="4"></div> <div class="rating-circle _5" id="5"></div> </div> 

$(document).ready(function() {
  $(".rating-circle").click(function() {
    // reset all
    $(this).parent().find('div').removeClass("rating-chosen");
    // Set selected
    $(this).addClass("rating-chosen");
  });
});

Working fiddle 工作提琴

$(this).index() - Gives the index of the clicked circle $(this).index() -给出被点击的圆的索引

$(this).toggleClass("rating-chosen"); - Toggles the class - adds class if it is not present, removes class if it is present. -切换类-如果不存在则添加类,如果不存在则删除类。

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

相关问题 我怎样才能 position 页面底部的元素? - How can I position an element at the bottom of the page? 如何识别 DOM 中的每个元素以及元素的顺序? - How can I identify every element in the DOM and the order of elements? 在Bootstrap中,如何识别“活动”导航元素? - In Bootstrap, how can I identify the “active” navigation element? 如何为一个元素生成一个随机的 position,使其不与 javascript 中不同元素的 position 重叠? - How can I generate a random position for an element so that it does not overlap with the position of a different element in javascript? 如何在react元素集合中识别单个div元素? - How to identify a single div element among set of elements in react? 如果数组的元素处于奇数或偶数位置,我如何在 Javascript 中查看? - How can I see in Javascript if the element of an array is in an odd or in an even position? 如何根据位置选择元素? - How can I select an element based on its position? 如何找出容器元素内的文字位置? - How can I find out text position inside container element? 如何获取js元素中存在的任何位置的textnodes - How can I get textnodes of any position present in element in js 如何显示带有单击元素位置的警报? - How can I display an alert with the position of the element that was clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM