原型中是否有与以下内容等效的内容?

发布于 2024-08-24 09:59:57 字数 282 浏览 8 评论 0原文

特别是关于 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 技术交流群。

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

发布评论

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

评论(1

撩人痒 2024-08-31 09:59:57

Live

Prototype 目前不提供“实时”支持。不过,您可以对事件委托执行类似的操作。您可以在其中监视父元素上的事件,然后在事件处理程序中找出事件实际发生在哪个子元素上:

$('theFormID').observe('click', handleFormClick);
function handleFormClick(event) {
    var element;

    // Get the element on which the click occurred
    element = event.findElement();
}

事件委托仅适用于冒泡事件(例如“单击”);我不知道 jQuery 的 live 东西是否也有同样的问题。

一件好事是您可以将 CSS 选择器传递到 Event#findElement< /code>

$('tbodyID').observe('click', handleTableClick);
function handleTableClick(event) {
    var element;

    // Get the element on which the click occurred
    element = event.findElement('tr');
}

...查找被点击的 tr,即使实际点击发生在 tdspantr 内的 code>。

正确使用事件委托可以极大地简化挂钩/取消挂钩元素,特别是在动态添加和删除元素时。

如果 Prototype 有一个提供“实时”支持的插件,我不会感到惊讶。

Parent

您提到您想要获取单击控件的父级。当您使用 Prototype 连接处理程序时,默认情况下它会进行设置,以便在您的处理程序中,this 引用您已设置事件处理程序的元素。例如,如果您有一个 id 为 foodiv

$('foo').observe('click', function(event) {
    // Here, `this` will refer to the `foo` element
});

那么您可以使用 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:

$('theFormID').observe('click', handleFormClick);
function handleFormClick(event) {
    var element;

    // Get the element on which the click occurred
    element = event.findElement();
}

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:

$('tbodyID').observe('click', handleTableClick);
function handleTableClick(event) {
    var element;

    // Get the element on which the click occurred
    element = event.findElement('tr');
}

...which finds the tr that was clicked, even if the actual click occurred within a span inside a td inside the tr.

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 a div with the id foo:

$('foo').observe('click', function(event) {
    // Here, `this` will refer to the `foo` element
});

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.)

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