最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - YUI get element by id method not working for numeric starting ids - Stack Overflow

programmeradmin1浏览0评论

I've just switched from YUI2 to YUI3. So, instead of using YAHOO.util.Dom.get(ID_OF_ELEMENT), I tried to use Y.one('#ID_OF_ELEMENT)'. It works fine for a div with id img123, but not with 123img or 123.

I tried also using Y.all but it didn't work. The only way I found to make it work, while still using YUI, was using Y.DOM.byId (shown as an alternative in YUI forum).

So what I did was to grab the element with the last and get the Node with first, like this:

Y.one(Y.DOM.byId(ID_OF_ELEMENT)).append(SOME_HTML_CONTENT);

I could not alone using only Y.DOM.byId because I needed to manipulate it's content as a Node.

So, is there a way to do that using only Y.one? Is that a YUI bug?

I have made a ment on that YUI forum entry, since I don't know if that's really a bug I could fill in YUI bug reporting tool.

I've just switched from YUI2 to YUI3. So, instead of using YAHOO.util.Dom.get(ID_OF_ELEMENT), I tried to use Y.one('#ID_OF_ELEMENT)'. It works fine for a div with id img123, but not with 123img or 123.

I tried also using Y.all but it didn't work. The only way I found to make it work, while still using YUI, was using Y.DOM.byId (shown as an alternative in YUI forum).

So what I did was to grab the element with the last and get the Node with first, like this:

Y.one(Y.DOM.byId(ID_OF_ELEMENT)).append(SOME_HTML_CONTENT);

I could not alone using only Y.DOM.byId because I needed to manipulate it's content as a Node.

So, is there a way to do that using only Y.one? Is that a YUI bug?

I have made a ment on that YUI forum entry, since I don't know if that's really a bug I could fill in YUI bug reporting tool.

Share Improve this question edited Feb 5, 2012 at 3:59 lucasarruda asked Feb 3, 2012 at 19:50 lucasarrudalucasarruda 1,4821 gold badge26 silver badges45 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8 +50

If you use html4:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Also see this link.

=== UPDATE ===

For html5:

In your example using Y.one(...) yui calls the browsers native query selector: querySelector(selector) (see here).
But not all browsers querySelector function accept all allowed html5 ids. E.g. the firfox10 native query selector fails for ids with starting digit (try this example in different browsers).
Why? Mozilla uses CSS2.1 specification for selectors:
Mozilla links in his querySelector documentation to the Selectors API Level 1.
In the first chapter "Abstract":

Selectors, which are widely used in CSS, are patterns that match against elements in a tree structure [SELECT][CSS21].

The [SELECT] links to the Selectors Level 3 and there in chapter 6.5 "ID selectors":

...
An ID selector contains a "number sign" (U+0023, #) immediately followed by the ID value, which must be an CSS identifiers.
...

From the linked css2.1 identifiers specification:

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

=== UPDATE ===

You can use following selector [id="123"]. E.g.:

YUI().use('node', function (Y) {
    Y.one('[id="123"]').on("click", function (e) {
        alert("Hello World!");
    });
});

Also see this example.

发布评论

评论列表(0)

  1. 暂无评论