开发者

Parse string value pair in javascript

开发者 https://www.devze.com 2023-03-16 20:02 出处:网络
I have an attribute of a list object in html like this: <ul> <li id=\"myID\" add-param=\"{\"type1\":\"myType1\",\"type2\":\"myType2\"}\">Test</li>

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/

0

精彩评论

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