开发者

What is the proper place to put AJAX-related content in a link

开发者 https://www.devze.com 2023-01-19 00:37 出处:网络
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 th

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()

0

精彩评论

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