Escape Quotes In HTML5 Data Attribute Using Javascript

前端 未结 9 2173
春和景丽
春和景丽 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条回答
  •  梦毁少年i
    2020-12-30 21:17

    Use the btoa method to set the data and the atob method to get it:

     $(document).data("test2",btoa('She said "WTF<\/abbr>" on last nights show.">'))
    

    Or simply dereference the string as a variable:

     var stringer = 'She said "WTF<\/abbr>" on last nights show.">'
    
     $(document).data("test2",stringer);
    

    References

    • HTML5 Living Standard, Web application APIs: Base64 utility methods

提交回复
热议问题