简体   繁体   中英

How can I normalise a JavaScript object to a DOM element with YUI3?

YUI2's Dom.get accepts both a DOM element or an id string as a parameter. In YUI3, Y.one is the replacement for Dom.get but it only accepts CSS selectors, not DOM elements. Is there a simple way, using YUI3, to normalise a JavaScript object to a DOM element?

To support the same signature as YAHOO.util.Dom.get you could do something like this:

var getNode = function(el) {
    return Y.one('#' + el) || new Y.Node(el);
};

Here's an example of the function above in use.

According to the API docs for Y.one it does accept a DOM element.

node a node or Selector

I think you can also pass a YUI 3 Node to it too. The only thing it doesn't do that DOM.get did is accept an id that isn't a CSS selector (eg 'foo' instead of '#foo').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM