aScript原型的功能类似于Jquery的现场跟踪动态DOM元

aScript原型的功能类似于Jquery的现场跟踪动态DOM元

本文介绍了有没有JavaScript原型的功能类似于Jquery的现场跟踪动态DOM元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Event.observe(window,"load",function() {
  $$(".elem_classs").findAll(function(node){
    return node.getAttribute('title');
  }).each(function(node){
    new Tooltip(node,node.title);
    node.removeAttribute("title");
  });
});

使用上面的方法,我可以检索的所有元素具有。elem_class并应用在他们身上一些JavaScript功能。但我有一个模式/弹出框里面有一些内容也有.elem_class这些不得到的findAll范围/每个因为他们被加载到DOM直通阿贾克斯。

Using above method, I can retrieve all elements having ".elem_class" and apply some javascript functions on them. But I have a modal/popup box which has some elements also having ".elem_class" and these dont get in the scope of findAll/each as they are loaded into the dom thru ajax.

我如何申请相同的动态加载的元素呢?我使用的原型库。 (我已经使用jQuery的直播功能,这使今后所有元素的轨道,但需要实现使用原型类似的东西)

How do I apply the same to dynamically loaded elements as well?I am using Prototype Library. (I have used JQuery's Live function which keeps track of all future elements, but need to achieve something similar using Prototype)

感谢。

推荐答案

据我所知,事件代表团机器人内置原型,但它不应该太难做你自己。只需添加一个处理程序,以观察其对时身体然后使用的,以检查是否你的选择相匹配。

As far as I know, event delegation is bot built into Prototype, but it shouldn't be too difficult to do on your own. Simply add a handler to observe the event on the body then use Event#findElement to check if it matches your selector.

下面是一个简单的功能,设置了代表团对您(负载运行此):

Here is a sample function that sets up the delegation for you (run this on load):

/**
 * event_type: 'click', 'keydown', etc.
 * selector: the selector to check against
 * handler: a function that takes the element and the event as a parameter
 */
function event_delegator(event_type, selector, handler) {
  Event.observe(document.body, event_type, function(event) {
    var elt = Event.findElement(event, selector);
    if (elt != document)
      handler(event, elt);
  });
}

您也许可以扩展单元来处理这个给你,精简一切。希望这有助于!

You could probably extend Element to handle this for you, streamlining everything. Hope this helps!

编辑:悬停事件(或mousein / mouseout事件)应该是一个很好的事件的工具提示。另外,不要在加载的所有元素,即使用事件委派时是不必要的。以下是有关事件委派链接到更多:http://www.sitepoint.com/blogs/2008/07/23/javascript-event-delegation-is-easier-than-you-think/

The hover event (or mousein/mouseout) should be a good event for a tooltip. Also, don't get all the elements on load, that's unnecessary when using event delegation. Here's a link to more about event delegation: http://www.sitepoint.com/blogs/2008/07/23/javascript-event-delegation-is-easier-than-you-think/

这篇关于有没有JavaScript原型的功能类似于Jquery的现场跟踪动态DOM元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 14:54