When you need a link to contain info for an AJAX call what is the proper place to put the info?
I've always put it in the rel attribute, but reading the documentation for rel it seems this isn't the proper place:
This attribute describes the relationship from the current document to the anchor specified by the href attribute. The value of this attribute is a space-separated list of link types.
You could parse the href and pull out the info you need:
<a class="store_link" href="/store/4/name-of-store">Name of Store</a>
$(".store_link").click( f开发者_运维问答unction() {
id = $(this).attr('href').split('/')[2];
});
But links won't always have hrefs
Is there a proper place to put this information?
I use non-standard HTML attributes, prefixed with data-
See this answer for more details. It won't validate, but it works perfectly. You need to decide how important 100% validation is to you.
jQuery allows you to store data against DOM objects in a much more elegant way.
See the data()
api here: http://api.jquery.com/jQuery.data/
Another way that is used often but can make your X/HTML non compliant is creating your own attributes.
eg. <div id="myDiv" someattr="lipsum dolor sit emet"> ... </div>
and then in your jQuery you can get the value by doing $('myDiv').attr('someattr')
You could use .data()
精彩评论