开发者

My first idea for a jQuery plugin

I keep thinking that the开发者_运维问答re should be a .id in jQuery instead of .attr('id').

Q: How do you write a plug-in, so that myObject.id returns myObject.attr('id')?


For sake of argument here's how you would do it.

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


You can already do something like this

$(".classDiv")[0].id

or write your own .id() plugins :)


Not sure you are specifically interested only for "id" or not, I am extending your question below.

considering the following fragment:

<p id="hello" another="ok" class="voila">Hello World</p>

I want to use:

alert( $("#hello").another ); // -> ok
alert( $("#hello").class ); // -> voila

currently we need to use $("#hello").attr("another") because $("#hello")[0].another will not work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜