Parse string value pair in javascript
I have an attribute of a list object in html like this:
<ul>
<li id="myID" add-param="{"type1":"myType1","type2":"myType2"}">Test</li>
</ul>
Having the list item with specified ID, how could I get the value myType1,myType2 by the key 开发者_C百科type, type2 ?
Thanks.
var json = JSON.parse($('#myID').attr('add-param'));
alert(json.type1) # myType1
Some old browsers don't have JSON.parse, you'll need to use this - https://github.com/douglascrockford/JSON-js/blob/master/json2.js
Sidenote: You should be using data attributes instead of defining your own custom attributes - http://ejohn.org/blog/html-5-data-attributes/
精彩评论