开发者

How to get the object's id?

How to get the id on an 开发者_如何学Cobject, i know the id is hola, but i need to get it during runtime

alert($('#hola').id);

The idea is this:

<script>

    (function ($) {

    $.fn.hello = function(msg) {
        alert('message ' + msg);
        alert('is from ' + this.id); // this.id doesn't work
    };

    })(jQuery);


    $('#hola').hello('yo');

</script>


Use attr() to read attributes:

alert($('#hola').attr('id'));


The most efficient approach would be:

this[0].id

this.attr("id") takes longer to achieve the same thing because many checks are made and different codepaths are followed based on the parameter passed. Depending on how often you call the function, there could be a significant difference on, say, a mobile browser with a slow processor.

You can read more about this here.


You can read it as atttibute:

alert($('#hola').attr('id'));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜