简体   繁体   中英

How can I get the ID, class or HTML tag of an element that calls my function?

I've been trying to use jQuery.fn.extend() and it's been working but I want to get the ID, class or html tag that called it. I can't know whether the string calling my fn is a tag, ID or class.

For example,

$('table').scrollify();

I want to be able to get the string 'table' not this inside my scrollify function

Here's my code:

 (function($) { $.fn.scrollify = function() { $('tbody').css({ 'width': Math.ceil($('thead').width() + 17) }); $('tbody > tr > td').css({ 'width': Math.ceil($('thead').width()) }); $('thead > tr > th').css({ 'width': $('tbody').width() / $('thead > tr > th').length }); $('tbody').css({ 'margin-top': $('thead').height() - 1 }); return this.get(0).scrollHeight > this.height(); }; })(jQuery); $('table').scrollify(); 

After some tweaking and "consoling" this a lot, I found out that I just need to do the following:

this.selector

Period.

You can use this.id to get id

this.prop("tagName"); to get tag name

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