Is it bad to add JSON on HTML data attribute?

前端 未结 5 1680
花落未央
花落未央 2021-01-31 16:05

Since HTML data attribute allows adding any custom data, I wonder if it is a good idea to include a set of JSON list as a data attribute?

5条回答
  •  攒了一身酷
    2021-01-31 16:45

    Technically you can, and I have seen several sites do this, but another solution is to store your JSON in a

    JS

    $(function () {
        var dataId = $("#x").data("data-id");
        var dataTag = $(dataId);
        var dataJson = dataTag.html(); // returns a string containing the JSON data
        var data = JSON.parse(dataJson);
    
        ...
    });
    

提交回复
热议问题