开发者

How to get the object's id?

开发者 https://www.devze.com 2023-02-28 23:23 出处:网络
How to get the id on an 开发者_如何学Cobject, i know the id is hola, but i need to get it during runtime

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

精彩评论

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