is it possible to create custom Identification attributes?
is it possible to create a identification attribute sort of a class or an id and assign it in jQuery selector with special character? for example I want an "identity" attribute to 开发者_开发问答be recognized as @ (or any other special character) same like an "id" attribute recognized as #
sort of
<div identity="myDiv"></div>
and Jquery
$("@myDiv").click(...);
Thanks!
Sure, use the the attribute-equals-selector
[docs].
$('[identity="myDiv"]').click(...
Note that this needs to look at every element on the page and check for the attribute.
You'll gain some performance if you select by class, or add a class to a group of elements, and narrow those down by your custom attribute.
$('.someClass[identity="myDiv"]').click(...
Or if all the potential elements are located in a container with an ID, then definitely narrow your selection down to descendants of that element.
$('#someId [identity="myDiv"]').click(...
or
$('#someId').find('[identity="myDiv"]').click(...
EDIT:
You can define custom filters in jQuery if you wish:
$.expr[':']['c'] = function( el, b, filter ) {
return el.getAttribute( 'identity' ) === filter[ 3 ];
};
var result = $(':c(myDiv)');
This simply uses the native getAttribute
to get the identity
attribute, and compare it to the value passed. If you need different processing, you can add it to the filter function.
The same rules apply about narrowing the selection for performance.
You can try something like this
$("div").find('[identity="myDiv"]').click(function(){ });
Use data-
attributes (jsfiddle as a proof):
<div data-identity="myDiv"></div>
and invoke it like that:
jQuery('[data-identity="myDiv"]').click(function(){
alert('You just clicked me!');
});
Do not try to change the way the original jQuery selector library (Sizzle) works - it uses CSS selectors and let it stay like that. Moreover, this was optimized and will be compatible in the future versions of jQuery if you do not change the selector library.
HTML is one kind of Mark-up Language
. In markup languages, you can define your own attributes as you need and as you like... But the only thing is that, there should be a convention between designers and web-browsers in order to understand how to parse specific attributes.
So, you are free to define your attributes and use them as you like...
What "patrick dw" wrote is correct, but i think its better to write it like
$('Div[identity="myDiv"]').click(...);
in order to make sure that you are working on your Div
, not any other element.
While I agree with everyone about the [identity=
stuff, another option would be to write a custom parameterised selector. Complete overkill, but it's an option, and would let you write something like
$('div:myidentity(myDiv)').click(...);
There's many ways to get selected the item as can be:
div = $("[identity='myDiv']");
but you can creat some pseudo slector by extending the expresion ":" in your case:
$.extend($.expr[':'],{
identity: function(a,i,m) {
return $(a).attr('identity') === m[3];
}
});
myDiv = $(":identity(myDiv)");
is more simple than the other way and you can use the selector you want.
精彩评论