jQuery 查找“选择器”
以下是我的代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为提供此功能的单击、焦点等事件编写您自己的包装器。编写这样的包装器相当简单。这是一种方法:
您的事件处理函数现在获取两个参数而不是一个。第一个是事件,第二个是用于绑定此事件的选择器。
用闭包包装它的优点是,您可以将多个单击事件绑定到具有不同选择器的同一元素,并且它们将一起工作。
查看示例。
Write your own wrappers for click, focus, .. events that provide this functionality. Writing such a wrapper is fairly trivial. Here's one way:
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.
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.
通常您会使用
selector
,但在这种情况下,this
没有选择器。您可以使用:当然,这将为每个子项显示相同的选择器,但并不是那么有用(也许是调试)。请注意,您可以更简单:
工作示例: http://jsfiddle.net/cmuTv/
Normally you'd use
selector
, but in this context,this
has no selector. You might use:Of course, that will show the same selector for each child, and not all that useful (maybe debugging). You can go simpler, mind you:
Working example: http://jsfiddle.net/cmuTv/
除了科比的回答之外,这通常是不可能的。这有点像问“给定以下循环,我如何再次获取原始数组?”
很明显,在这种情况下,您无法仅通过
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?"
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 afor
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.