Escape Quotes In HTML5 Data Attribute Using Javascript

前端 未结 9 2199
春和景丽
春和景丽 2020-12-30 20:47

I\'m using jQuery\'s .data() to work with custom HTML5 data attributes where the value of the attribute needs to be able to contain both single quotes and doubl

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-30 21:37

    Use encodeURI to escape quotation marks in your JSON object. Parse the string with decodeURI.

    var popup = document.getElementById('popup'),
        msgObj = JSON.parse(decodeURI(popup.dataset.message));
    
    console.log(msgObj);

提交回复
热议问题