Passing JSON data from php to html-data attribute and then to Javascript

后端 未结 1 1448
礼貌的吻别
礼貌的吻别 2021-01-01 10:48

i\'m creating a plugin in which the user adds custom settings in data- attribute in HTML. Settings are in JSON format. I\'m using these settings in Javascript.<

相关标签:
1条回答
  • 2021-01-01 11:07

    You need to escape data and handle special characters.

    <div data-side="front" data-params="<?php echo htmlspecialchars(json_encode($dataParams), ENT_QUOTES, 'UTF-8'); ?>">
    

    And get it with jQuery:

    $('[data-side="front"]').data('params'); // object
    
    0 讨论(0)
提交回复
热议问题