原型中是否有与以下内容等效的内容?
特别是关于 live 的部分......
jQuery('.something').live('click', function() {
jQuery(this).parent('form').reset();
});
我想这也可能有效,尽管我对父函数很好奇:
jQuery('.something').live('click', function() {
this.form.reset();
});
Particularly the bit about live....
jQuery('.something').live('click', function() {
jQuery(this).parent('form').reset();
});
I suppose this might work as well, though I am curious about the parent function:
jQuery('.something').live('click', function() {
this.form.reset();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Live
Prototype 目前不提供“实时”支持。不过,您可以对事件委托执行类似的操作。您可以在其中监视父元素上的事件,然后在事件处理程序中找出事件实际发生在哪个子元素上:
事件委托仅适用于冒泡事件(例如“单击”);我不知道 jQuery 的 live 东西是否也有同样的问题。
一件好事是您可以将 CSS 选择器传递到
Event#findElement< /code>
:
...查找被点击的
tr
,即使实际点击发生在tdspan
内tr
内的 code>。正确使用事件委托可以极大地简化挂钩/取消挂钩元素,特别是在动态添加和删除元素时。
如果 Prototype 有一个提供“实时”支持的插件,我不会感到惊讶。
Parent
您提到您想要获取单击控件的父级。当您使用 Prototype 连接处理程序时,默认情况下它会进行设置,以便在您的处理程序中,
this
引用您已设置事件处理程序的元素。例如,如果您有一个 id 为foo
的div
:那么您可以使用 Prototype 的 Element#up 获取父元素。 (如果您使用事件委托并且首先将事件挂接到父级,当然只需直接使用
this
,如果您还需要获取引用对于被单击的内容,请使用上面的#findElement
。)Live
Prototype doesn't currently have "live" support. You can do something similar with event delegation, though. That's where you watch for the event on a parent element, and then in the event handler find out which child element the event actually happened on:
Event delegation is only possible with events that bubble (like 'click'); I don't know if jQuery's live stuff has that same issue.
One nice thing is that you can pass CSS selectors into
Event#findElement
:...which finds the
tr
that was clicked, even if the actual click occurred within aspan
inside atd
inside thetr
.Proper use of event delegation can dramatically simplify hooking/unhooking elements, particular when you're adding and removing elements dynamically.
I wouldn't be surprised if there's a plug-in for Prototype that does "live" support.
Parent
You mentioned that you want to get the parent of the clicked control. When you hook up a handler with Prototype, by default it sets things up so that within your handler,
this
references the element on which you've set the event handler. So for instance, if you have adiv
with the idfoo
:You can then use Prototype's Element#up to get the parent element. (If you're using event delegation and you hook the event on the parent in the first place, of course just use
this
directly and if you also need to get a reference to the thing that was clicked, use#findElement
as above.)