jQuery 查找“选择器”

发布于 2024-09-10 02:43:54 字数 451 浏览 2 评论 0原文

以下是我的代码

HTML

<div id = "content" >
  <div class = "child" >Hello !! </div>
</div>

Javascript

$(function() {
  $('#content .child').click(function() {
          //print the selected jQuery parameters
  });
});

我需要捕获传递给上面代码中的 jQuery 函数的参数, 我想将输出打印为 #content .child

谢谢 !!

following is my code

HTML :

<div id = "content" >
  <div class = "child" >Hello !! </div>
</div>

Javascript :

$(function() {
  $('#content .child').click(function() {
          //print the selected jQuery parameters
  });
});

I need to capture parameters which I pass to jQuery function in above code,
I want to print the output as #content .child.

Thank you !!

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

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

发布评论

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

评论(3

疑心病 2024-09-17 02:43:54

为提供此功能的单击、焦点等事件编写您自己的包装器。编写这样的包装器相当简单。这是一种方法:

jQuery.fn.addClick = function(fn) {
    var selector = this.selector; // capture the selector here
    this.click(function(event) { // write a wrapper for the click handler
        fn(event, selector); // call the user's handler and pass it the selector
    });
};

您的事件处理函数现在获取两个参数而不是一个。第一个是事件,第二个是用于绑定此事件的选择器。

$("#content .child").addClick(function(e, selector) {
    alert(selector); // "#content .child"
});

用闭包包装它的优点是,您可以将多个单击事件绑定到具有不同选择器的同一元素,并且它们将一起工作。

查看示例

Write your own wrappers for click, focus, .. events that provide this functionality. Writing such a wrapper is fairly trivial. Here's one way:

jQuery.fn.addClick = function(fn) {
    var selector = this.selector; // capture the selector here
    this.click(function(event) { // write a wrapper for the click handler
        fn(event, selector); // call the user's handler and pass it the selector
    });
};

Your event handler function now gets two params instead of one. First is event, and the second is the selector that was used to bind this event.

$("#content .child").addClick(function(e, selector) {
    alert(selector); // "#content .child"
});

The advantage of wrapping it up with closures is that you can bind multiple click events to the same element with different selectors, and they will all work together.

See an example.

-小熊_ 2024-09-17 02:43:54

通常您会使用selector,但在这种情况下,this 没有选择器。您可以使用:

var div = $('#content .child');
div.click(function() {
    alert(div.selector);
});

当然,这将为每个子项显示相同的选择器,但并不是那么有用(也许是调试)。请注意,您可以更简单:

var selector = '#content .child';
$(selector).click(function() {
    alert(selector);
});

工作示例: http://jsfiddle.net/cmuTv/

Normally you'd use selector, but in this context, this has no selector. You might use:

var div = $('#content .child');
div.click(function() {
    alert(div.selector);
});

Of course, that will show the same selector for each child, and not all that useful (maybe debugging). You can go simpler, mind you:

var selector = '#content .child';
$(selector).click(function() {
    alert(selector);
});

Working example: http://jsfiddle.net/cmuTv/

空心空情空意 2024-09-17 02:43:54

除了科比的回答之外,这通常是不可能的。这有点像问“给定以下循环,我如何再次获取原始数组?”

var array = [1, 2, 3, 4, 5];
for (i = 0; i < array.length; i++) {
    var a = array[i];
    alert( /* how do I get the original array given only 'a'? */ );
}

很明显,在这种情况下,您无法仅通过 a 获取原始数组。但可能不清楚的是,您的情况也是如此。

这是因为对 .click() 的调用本质上变成了对 .each() 的调用,而这基本上只是一个 for 循环每个匹配的元素。当您查看该集合中的单个元素时,无法再次获取原始集合。

Apart from Kobi's answer, it's not generally possible. It's kind of like asking "given the following loop how do I get the original array back again?"

var array = [1, 2, 3, 4, 5];
for (i = 0; i < array.length; i++) {
    var a = array[i];
    alert( /* how do I get the original array given only 'a'? */ );
}

It's clear that in this case, you can't get the original array back given only a. But what might not be clear is that the same is true in your case as well.

This is because a call to .click() is essentially turned into a call to .each() and that's basically just a for loop around each of the matched elements. When you're looking at a single element in that collection, there's no way to get the original collection back again.

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