从单个链接传递大量数据的最佳方式是什么?这是个好主意还是有更好的方法?
<a href="{ 'id':'1', 'foo':'bar', 'something':'else' }" id="myLnk" > Click </a>
$('#myLnk).click( function(event){
event.preventDeafult();
var data = $(this).attr('href')
console.log( $.parseJSON( response ) );
})
谢谢!
最佳答案
最好的方法可能是使用 custom data attributes在你的 anchor 标签中。
例如:
<a href="#" data-json="{ 'id':'1', 'foo':'bar', 'something':'else' }">Click</a>
然后您可以使用 jQuery 从 JavaScript 轻松访问此数据 data()功能。
或者,您可以将其拆分为单独的属性,而不是将所有数据捆绑在一个 JSON block 中:
<a id="myLnk"
data-id="1"
data-foo="bar"
data-something="else">
然后可以像这样使用 jQuery 检索这些:
$("#myLnk").data("id")
$("#myLnk").data("foo")
$("#myLnk").data("something")
关于javascript - 从单个 anchor 标记传递大量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6085354/