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.
精彩评论