如何获取调用我的函数的元素的 ID、类或 HTML 标记

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

本文关键字:ID 元素 类或 标记 HTML 函数 我的 何获取 获取 调用      更新时间:2023-09-26

我一直在尝试使用jQuery.fn.extend()并且它一直在工作,但我想获取调用它的ID,类或html标签。我不知道调用我的fn的字符串是标签、ID 还是类。

例如

$('

table').scrollify();

我希望能够在我的scrollify函数中获取字符串'table'而不是this

这是我的代码:

(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();

经过一些调整和"安慰"this,我发现我只需要执行以下操作:

this.selector

时期。

您可以使用this.id来获取id

获取标记名称this.prop("tagName");