Escape Quotes In HTML5 Data Attribute Using Javascript

前端 未结 9 2169
春和景丽
春和景丽 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:30

    For it to be proper html, you have to escape the troublesome characters. I'd escape them with HTML entities. This means that whatever tool is being used to input this information would have have to store them properly and/or the tool retrieving them on the back end would have to escape them.

    Then if you want to use them in your JS, you'd have to run some find-and-replace functions to convert the characters back into HTML and quotes.

    Most back-end dev languages have some sort of 'htmlescape/unescape' functionality, so that shouldn't be to hard.

    To unescape it via jQuery, here's something found via a quick Google: http://www.naveen.com.au/javascript/jquery/encode-or-decode-html-entities-with-jquery/289

提交回复
热议问题