如何获取对象的id?

如何获取对象的id,我知道id是hola,但我需要在运行时获取它

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

这个想法是这样的:

  (function ($) { $.fn.hello = function(msg) { alert('message ' + msg); alert('is from ' + this.id); // this.id doesn't work }; })(jQuery); $('#hola').hello('yo');  

使用attr()读取属性:

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

最有效的方法是:

 this[0].id 

this.attr("id")需要更长时间才能实现相同的function,因为会进行许多检查,并根据传递的参数执行不同的代码路径。 根据您调用该function的频率,例如,具有慢速处理器的移动浏览器可能存在显着差异。

你可以在这里阅读更多相关信息。

您可以将其视为atttibute:

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