点击函数上有多个选择器,如何知道点击了哪个选择器
Multiple selectors on click function, how to know which selector was clicked?
Jquery新增。我在一个点击函数上有四个选择器,请参阅下面的代码,这是因为我希望在点击时对所有选择器都产生相同的效果,而不是为所有四个选择器创建一个点击功能。然而,当单击时,我希望函数识别被单击的选择器,这样我就可以对其中一个选择器执行操作,这就是为什么我使用id而不是类。更准确地说,我想将单击的选择器上的CSS设置为更高的z索引值,这样它就不会受到即将发生的效果的影响,在这种情况下,我希望单击的选择器的z索引为10。
我试着使用if语句,见下文,但它不起作用,有人知道怎么做吗?
<div id="1"></div>
<div id="2"></div>
<div id="3"></div>
<div id="4"></div>
我的jQuery尝试:
$(document).ready(function(e) {
var effect = $("#slide");
$("#1, #2, #3, #4").click(function() {
effect.animate({width: "1100px"}, 200, "easeOutQuint");
$("#2").data('clicked', true);
if ($("#2").data('clicked')) {
$("#2").css("z-index", "10");
}
});
});
使用this
关键字:
$(document).ready(function(e) {
var effect = $("#slide");
$("#1, #2, #3, #4").click(function() {
effect.animate({width: "1100px"}, 200, "easeOutQuint");
$(this).data('clicked', true);
if ($(this).data('clicked')) {
$(this).css("z-index", "10");
}
});
});
您可以简单地在click
回调中使用this
来确定单击的元素:
$("#1, #2, #3, #4").click(function() {
effect.animate({width: "1100px"}, 200, "easeOutQuint");
$(this).data('clicked', true);
if ($(this).data('clicked')) {
$(this).css("z-index", "10");
}
});
但是,为了使其工作,您不需要使用ID。如果可能的话,最好使用一个公共类而不是ID:
<div class="myClass"></div>
<div class="myClass"></div>
<div class="myClass"></div>
<div class="myClass"></div>
在JavaScript中:
$(document).ready(function(e) {
var effect = $("#slide");
$(".myClass").click(function() {
effect.animate({width: "1100px"}, 200, "easeOutQuint");
$(this).data('clicked', true);
if ($(this).data('clicked')) {
$(this).css("z-index", "10");
}
});
});
请注意,您的if ($(this).data('clicked'))
条件没有多大意义。由于您在条件之前将clicked
设置为true
,因此它将始终为true
。
这可能会对您有所帮助,或者您也可以使用开关情况
$("#1, #2, #3, #4").click(function() {
if($(this).attr("id") == "1"){
//do sth
}else if($(this).attr("id") == "2"){
//do sth
}else{
//
}
});
OR
使用event.target
:
$(document).ready(function(e) {
var effect = $("#slide");
$("#1, #2, #3, #4").click(function(event) {
effect.animate({width: "1100px"}, 200, "easeOutQuint");
$("#2").data('clicked', true);
if($(event.target).attr("id") == "2" ){
$("#2").css("z-index", "10");
}
});
});
根据@Spencer Wieczorek的评论进行编辑,如果你希望点击的输入具有10的z-index
,那么:
$(document).ready(function(e) {
var effect = $("#slide");
$("#1, #2, #3, #4").click(function(event) {
effect.animate({width: "1100px"}, 200, "easeOutQuint");
$("#2").data('clicked', true);
$(event.target).css("z-index", "10");
});
});
您不需要将id添加到所有的div中,只需要将其添加到z索引中,然后使用jquery attr()
函数
获取当前点击项目的id($(this)
指当前点击的项目),如果条件为true,则添加z索引试试这个:
<div></div>
<div id="zindex"></div>
<div></div>
<div></div>
$(document).ready(function(e) {
var effect = $("#slide");
$("div").click(function() {
effect.animate({width: "1100px"}, 200, "easeOutQuint");
if ($(this).attr('id') == "zindex") {
$(this).css("z-index", "10");
}
});
});
请参阅jsfiddle:https://jsfiddle.net/596w1hwr/
相关文章:
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 在动态创建的元素上获取对特定选择器的引用
- AngularJS-在JSON选择器中使用变量名
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- jQuery最近父级的数据属性选择器
- 为什么不'在JQuery中找到第二个css选择器的工作
- 为什么 .focus() 不起作用,而 .css(“color”,“red”) 在同一个选择器上起作用
- jquery IE7中的第n个选择器
- Webdriver.io pageObject模式-通过传递参数来定义元素选择器
- jquery日期选择器年份范围默认值
- jquery日期选择器失去了交互性
- JavaScript id 选择器不起作用?不知道出了什么问题
- 点击函数上有多个选择器,如何知道点击了哪个选择器
- 如果我不删除事件处理程序'我不知道用于创建此处理程序的原始选择器
- 如何知道jquery插件中的选择器
- 不知道为什么日期选择器小部件不能工作,也不知道为什么id选择器不能自动填充结果字段
- 在任何时间日期选择器中以编程方式设置时区偏移量
- 最快的选择器知道前 2 个父母
- 如何知道何时输入有一个:无效的选择器