开发者

crossbrowser unique identifier for a html tag

Is there some kind of unique identifier 开发者_如何学Cfor a HTML tag in a page?

I see in Mozilla there is the uid, is it cross browser? (I do not really care about IE6...)

I am also aware of Unique identifier for HTML elements They missed the uid there


No, there is no cross browser, future proof uid, see allowed attributes by HTML5


I found the attribute uid to work crossbrowser.


Not knowing exactly what you're looking for, here's a bit of jQuery that will take a selector and give a data-uid attribute to each match that resembles the asp.net ClientID.

$("div").each(function() {
    var uid = $.map($(".findMe").parentsUntil('body').andSelf(), function(o, i) {
        return o.id || o.tagName.toLowerCase() + $(o).prevAll(o.tagName).length;
    }).join('_');
    $(this).data("uid", uid);
});


IE has the uniqueID property that will automatically generate a unique id for each element. I've also created a shim for other browsers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜