开发者

Select attribute of root element in jQuery

开发者 https://www.devze.com 2023-01-27 03:07 出处:网络
How do you select the attribute of a \"root\" level ele开发者_JAVA技巧ment in jQuery? The following doesn\'t work (returns undefined):

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

精彩评论

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