我对jQuery插件的第一个想法

Phi*_*enn 1 jquery jquery-plugins

我一直认为jQuery中应该有一个.id而不是.attr('id').

问:你如何编写插件,以便myObject.id返回myObject.attr('id')?

Ray*_*nos 6

为了争论,你可以这样做.

// extend jQuery object with your id
$.fn.id = function() {
    // return the id of the first DOM element.
    return this[0].id;
}
Run Code Online (Sandbox Code Playgroud)