如何在jQuery中获取函数调用的div的ID?

时间:2021-11-05 19:35:17

I have a jquery function ('rater') that I call this way:

我有一个jquery函数('rater'),我这样称呼:

<div id="someID"></div>

<script type="text/javascript">
    $('#someID').rater({options});
</script>

I want to get the ID ('someID' in this case) so then I can use it inside the function as a variable.

我想获取ID(在这种情况下为'someID'),然后我可以在函数内部将其用作变量。

Then I use it in something like this:

然后我用这样的东西:

if (??? == 'someID') { do something }

How can I do it??

我该怎么做??

5 个解决方案

#1


Retrieving the selector used to call the plugin:

检索用于调用插件的选择器:

jQuery.fn.myPlugin = function() {
    alert( this.selector );
};

You can access the selector as a property of the jQuery object.

您可以将选择器作为jQuery对象的属性进行访问。

So, if I called the plugin like this:

所以,如果我这样调用插件:

$('div #something').myPlugin();

Then "div #something" would be alerted.

然后会提醒“div #something”。


Retrieving an element's ID from within a plugin:

从插件中检索元素的ID:

jQuery.fn.myPlugin = function() {
    alert( this.attr('id') );
};

#2


Not sure why you're trying to do this, but...

不知道你为什么要这样做,但......

// create jQuery plugin method: "rater"
jQuery.fn.rater = function() 
{
  // `this` is a jQuery object: process each matched element
  return this.each(function()
  {
    // `this` is a single matched element. Process it, somehow...
    if ( this.id == "someID" )
    {
      // do something special
    }

    // normal rater logic, whatever that might be
  });
};

#3


Are you looking for selector?

你在寻找选择器吗?

After reading your edited question again, it sounds as though you want the string of the id passed through.

再次阅读您编辑过的问题后,听起来好像您希望传递的ID字符串。

<div id="someID"></div>

<script type="text/javascript">
    $("#someID").rater({options},$("#someID").selector);
</script>

For efficiency sake, you can store the $("#someID") off in a variable if you like so you only do the query once.

为了提高效率,如果您愿意,可以将$(“#someID”)存储在变量中,这样您只需执行一次查询。

#4


This will look for the closest "div" called, then get its attribute.

这将查找最近的“div”,然后获取其属性。

if ($(this).closest("div").attr("id") == "someId") {
    // do stuff
}

#5


if( $(this).attr('id') == 'someID' ) {
  // do stuff
}

#1


Retrieving the selector used to call the plugin:

检索用于调用插件的选择器:

jQuery.fn.myPlugin = function() {
    alert( this.selector );
};

You can access the selector as a property of the jQuery object.

您可以将选择器作为jQuery对象的属性进行访问。

So, if I called the plugin like this:

所以,如果我这样调用插件:

$('div #something').myPlugin();

Then "div #something" would be alerted.

然后会提醒“div #something”。


Retrieving an element's ID from within a plugin:

从插件中检索元素的ID:

jQuery.fn.myPlugin = function() {
    alert( this.attr('id') );
};

#2


Not sure why you're trying to do this, but...

不知道你为什么要这样做,但......

// create jQuery plugin method: "rater"
jQuery.fn.rater = function() 
{
  // `this` is a jQuery object: process each matched element
  return this.each(function()
  {
    // `this` is a single matched element. Process it, somehow...
    if ( this.id == "someID" )
    {
      // do something special
    }

    // normal rater logic, whatever that might be
  });
};

#3


Are you looking for selector?

你在寻找选择器吗?

After reading your edited question again, it sounds as though you want the string of the id passed through.

再次阅读您编辑过的问题后,听起来好像您希望传递的ID字符串。

<div id="someID"></div>

<script type="text/javascript">
    $("#someID").rater({options},$("#someID").selector);
</script>

For efficiency sake, you can store the $("#someID") off in a variable if you like so you only do the query once.

为了提高效率,如果您愿意,可以将$(“#someID”)存储在变量中,这样您只需执行一次查询。

#4


This will look for the closest "div" called, then get its attribute.

这将查找最近的“div”,然后获取其属性。

if ($(this).closest("div").attr("id") == "someId") {
    // do stuff
}

#5


if( $(this).attr('id') == 'someID' ) {
  // do stuff
}