开发者

Select attribute of root element in jQuery

How do you select the attribute of a "root" level ele开发者_JAVA技巧ment in jQuery?

The following doesn't work (returns undefined):

jQuery(document).ready(function() {

    somehtml = "<a href='http://example.com'>An example</a>";
    theurl = jQuery('a',somehtml).attr('href');
    alert(theurl);

}

Any idea what I'm missing? I'm sure it's something obvious about root level elements ...

Many thanks in advance, Gav


You could do:

jQuery(document).ready(function() {

    somehtml = "<a href='http://example.com'>An example</a>";
    theurl = $(somehtml).attr('href');
    alert(theurl);

});

What I did was to construct a jQuery object out of the HTML you had, and then directly access the attr()-function for that.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜