Escape Quotes In HTML5 Data Attribute Using Javascript

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

    There is no way around it, you have to escape the values properly, or the HTML can't be parsed properly. You can't use Javascript to correct the code after it is parsed, because then it has already failed.

    Your example with proper HTML encoding would be:

    You can't use backslash to escape characters, because it's not Javascript code. You use HTML entities to escape characters in HTML code.

    If you can't control how the data is input, then you are screwed. You simply have to find a way to take control over it.

提交回复
热议问题