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

99封情书 提交于 2019-12-25 04:38:09

问题


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?


回答1:


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.




回答2:


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 (e.g. 'foo' instead of '#foo').



来源:https://stackoverflow.com/questions/4892051/how-can-i-normalise-a-javascript-object-to-a-dom-element-with-yui3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!