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

发布于 2024-10-05 04:34:35 字数 251 浏览 2 评论 0 原文

这是一个我无法弄清楚的小问题。我确信有人可以立即回答:

有多个选择器,例如

$('a.button, span.xyz, a.another').click(function(e) {
   var clicked_element = ???;
});

,我如何找出实际单击了哪个选择器?我需要像 $(clicked_element)... 一样使用它。

谢谢。

Here's a very small issue that I was unable to figure out. I'm sure someone can answer in no time:

Having multiple selectors like

$('a.button, span.xyz, a.another').click(function(e) {
   var clicked_element = ???;
});

, how can I figure out which selector was actually clicked? I need to use it like $(clicked_element)....

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清君侧 2024-10-12 04:34:35

使用 $(this) 将为您提供被单击的元素..并使用 is() 可以帮助您确定单击的内容。

$('a.button, span.xyz, a.another').click(function(e) {
  if ($(this).is("a.button")) {
    alert("a.button was clicked");
  } else if ($(this).is("span.xyz")) {
    alert("span.xyz was clicked");
  } else if ($(this).is("a.another")) {
    alert("a.another was clicked");
  }
});

编辑:

当我写下这个答案时,似乎有更好的方法。 Patrick DW 的评论引起了我的兴趣,我想了解更多。他的澄清在这里 jQuery - 在 a 中组合选择器的问题单个事件处理程序

这将是一种更好的方法

$("a.button").click(function(e) { ... });
$("span.xyz").click(function(e) { ... });
$("a.another").click(function(e) { ... });

据我了解,如果您的目标是将通用功能放在一个位置,那么这就是应该如何处理它

function commonFunctionality(elementSelector) {
  // common code for all elements here or at the end

  switch (elementSelector) {
    case "a.button":
      //do stuff for a.button only;
      break;
    case "span.xyz":
      //do stuff for span.xyz only;
      break;
    case "a.another":
      //do stuff for a.another only;
      break;
  }

  // common code for all elements
}


$("a.button").click(function(e) { ... });
$("span.xyz").click(function(e) { ... });
$("a.another").click(function(e) { ... });

Using $(this) will get you the element that was clicked.. and using is() can help you determine what was clicked.

$('a.button, span.xyz, a.another').click(function(e) {
  if ($(this).is("a.button")) {
    alert("a.button was clicked");
  } else if ($(this).is("span.xyz")) {
    alert("span.xyz was clicked");
  } else if ($(this).is("a.another")) {
    alert("a.another was clicked");
  }
});

Edited:

As I wrote up this answer it seems there is a better approach. Patrick DW's comment intrigued me and I wanted to know more. His clarification is here jQuery - Issues with combining selectors in a single event handler

This would be a better approach

$("a.button").click(function(e) { ... });
$("span.xyz").click(function(e) { ... });
$("a.another").click(function(e) { ... });

As I understand it if your goal was to place common functionality in one spot then this is how it should be handled

function commonFunctionality(elementSelector) {
  // common code for all elements here or at the end

  switch (elementSelector) {
    case "a.button":
      //do stuff for a.button only;
      break;
    case "span.xyz":
      //do stuff for span.xyz only;
      break;
    case "a.another":
      //do stuff for a.another only;
      break;
  }

  // common code for all elements
}


$("a.button").click(function(e) { ... });
$("span.xyz").click(function(e) { ... });
$("a.another").click(function(e) { ... });
蓝咒 2024-10-12 04:34:35

触发事件的元素在函数中以 $(this) 的形式提供,即

$('a.button, span.xyz, a.another').click(function(e) {
   var clicked_element = $(this);
});

您还可以使用 is():

if ($(this).is('a.button'))) { ... }

The element which triggers an event is available within the function as $(this), i.e.

$('a.button, span.xyz, a.another').click(function(e) {
   var clicked_element = $(this);
});

You can also test to identify if the element matches a particular selector using is():

if ($(this).is('a.button'))) { ... }
酷到爆炸 2024-10-12 04:34:35

如果这些元素具有不同的功能,我会同意帕特里克和德克斯特关于分离这些元素的观点。但如果您要使用此方法,请尝试使用一些内置的 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
});

I would agree with Patrick and Dexter about separating these elements if they have different functionality. But if you are going to use this method, try using some of the built in javascript methods. I don't know what the HTML markup looks like, but assuming this markup, you can try the script below:

<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
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文