简体   繁体   中英

How to get the object's id?

How to get the id on an object, 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>

使用attr()来读取属性:

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 .

您可以将其视为atttibute:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM