如何获得对象的ID,我知道ID是HOLA,但我需要在运行过程中得到它如何获取对象的ID?
alert($('#hola').id);
的想法是这样的:
<script>
(function ($) {
$.fn.hello = function(msg) {
alert('message ' + msg);
alert('is from ' + this.id); // this.id doesn't work
};
})(jQuery);
$('#hola').hello('yo');
</script>
在一个jQuery插件中,'this'是指代表所选元素的jQuery对象。在这种情况下,使用'this.attr('id')'作为@Joachim发布,可能是最简单的方法(和'$('#hola').id'不能用btw)。 – 2011-04-26 11:58:03