Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 多个选择器:确定触发选择器?_Javascript_Jquery Selectors_Jquery - Fatal编程技术网

Javascript 多个选择器:确定触发选择器?

Javascript 多个选择器:确定触发选择器?,javascript,jquery-selectors,jquery,Javascript,Jquery Selectors,Jquery,这里有一个非常小的问题,我无法解决。我相信有人很快就能回答: 具有多个选择器,如 $('a.button, span.xyz, a.another').click(function(e) { var clicked_element = ???; }); ,如何确定实际单击了哪个选择器?我需要像使用$(单击元素)…一样使用它 谢谢。触发事件的元素在函数中可用作$(this),即 $('a.button, span.xyz, a.another').click(function(e) {

这里有一个非常小的问题,我无法解决。我相信有人很快就能回答:

具有多个选择器,如

$('a.button, span.xyz, a.another').click(function(e) {
   var clicked_element = ???;
});
,如何确定实际单击了哪个选择器?我需要像使用
$(单击元素)…
一样使用它


谢谢。

触发事件的元素在函数中可用作
$(this)
,即

$('a.button, span.xyz, a.another').click(function(e) {
   var clicked_element = $(this);
});
您还可以使用以下方法进行测试以确定元素是否与特定选择器匹配:

使用$(this)将获得单击的元素。。使用可以帮助您确定单击了什么

$('a.button,span.xyz,a.other')。单击(函数(e){
if($(this).is(“a.button”)){
警报(“a.按钮被点击”);
}如果($(this).is(“span.xyz”)){
警报(“已单击span.xyz”);
}else if($(this).is(“a.other”)){
警报(“另一个被点击”);
}
});

编辑:

当我写下这个答案时,似乎有更好的方法。Patrick DW的评论引起了我的兴趣,我想知道更多。他的澄清就在这里

这将是一个更好的方法

$(“a.button”)。单击(函数(e){…});
$(“span.xyz”)。单击(函数(e){…});
$(“a.other”)。单击(函数(e){…});
据我所知,如果您的目标是将通用功能放在一个位置,那么这就是应该如何处理它

函数公共功能(elementSelector){
//此处或末尾所有元素的通用代码
开关(元件选择器){
案例“a.按钮”:
//只为按钮做事情;
打破
案例“span.xyz”:
//仅为span.xyz执行操作;
打破
案例“a.另一个”:
//只为别人做事;
打破
}
//所有元素的通用代码
}
$(“a.button”)。单击(函数(e){…});
$(“span.xyz”)。单击(函数(e){…});
$(“a.other”)。单击(函数(e){…});

如果Patrick和Dexter有不同的功能,我同意他们关于分离这些元素的意见。但是如果要使用此方法,请尝试使用一些内置javascript方法。我不知道HTML标记是什么样子的,但假设使用此标记,您可以尝试以下脚本:

<a class="button" href="http://somesite.com">Some Site</a>
<span class="xyz">span</span>
<a class="another" href="/topics/topic2.html">Topic 2</a>

$('a.button, span.xyz, a.another').click(function(e) {
   var clicked = 'a.another';
   if (this.tagName == "SPAN") { clicked = 'span.xyz'; }
   if (this.href.match('somesite')) { clicked = 'a.button'; }
   // Do something
});

跨度
$('a.button,span.xyz,a.other')。单击(函数(e){
var clicked='a.other';
如果(this.tagName==“SPAN”){clicked='SPAN.xyz';}
如果(this.href.match('somesite'){clicked='a.button';}
//做点什么
});

对。。。但是我需要在if条件下使用它,并且希望避免使用
hasClass
等等。我需要做一些类似于
if(clicked_-element=='a.button'){}的事情,否则if(clicked_-element=='span.xyz')
。如果不清楚,很抱歉。如果你真的想为每个元素做不同的事情,我建议你为每个元素绑定一个不同的点击处理程序@我同意德克斯特的观点。如果这些元素具有不同的功能,那么我将分配不同的处理程序,而不是在每个事件中增加测试每个处理程序的开销。如果有一些通用的功能,那么将其应用到它自己的函数中,并从各个处理程序调用它。如果这些都在同一个容器中,那么事件委派可能是更好的解决方案。这样,您就可以用测试元素的开销换取更少的处理程序。@bobsoap。不客气,请不要忘记接受正确的答案。问题在于,它有效地结合了
.delegate()
遭受的低效性(针对选择器进行测试)和分配多个冗余处理程序的低效性。所以你在两个世界中都会遇到最坏的情况。@Patrick DW。。。你能再解释一下吗。我相信你说我应该把点击处理程序分解成单独的语句?这是正确的吗?如果是,为什么?@Patrick DW。。。我在这里发布了一个问题,想从您那里得到关于这些低效率的答案。你能给我解释得更详细些吗@bobsoap。。。阅读我编辑过的答案会很好,因为它提供了对您的问题更深入的见解。如果你能在@Patrick DW回答这个问题时给他一些信任,那也会很好。这与我在约翰附加职位上的回答相似。我完全同意,使用本机dom脚本编写这种类型的脚本速度非常快,而且通常也更容易理解。
<a class="button" href="http://somesite.com">Some Site</a>
<span class="xyz">span</span>
<a class="another" href="/topics/topic2.html">Topic 2</a>

$('a.button, span.xyz, a.another').click(function(e) {
   var clicked = 'a.another';
   if (this.tagName == "SPAN") { clicked = 'span.xyz'; }
   if (this.href.match('somesite')) { clicked = 'a.button'; }
   // Do something
});