[英]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");
});
});
$(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.