<script>
var html = '<select><option>Mike\'s</option></select>';
</script>
solve the problem within php
str_replace("'", "/'", $str);
solve the problem in js, of course, if the user enters `, it will also report an error. It's better to deal with it in php
.
var html = `<select><option>Mike's</option></select>`;
escape with backslash:
'Mike\'s'
you can also use double quotes:
"Mike's"
double quotes are not escaped in single quote string , and single quotation marks are not escaped in double quote string .
but note that there is a slight difference between single quotation marks and double quotation marks. For example, variables can be used directly in double quotation marks:
$a='Mike';
"$a's"; //Mike's
there are also more escape characters that can be used in double quotes, such as \ n
line breaks, and so on.
the user enters without processing , because it has been processed internally in PHP, where the single quotation marks are characters and will not be paired with the single quotation marks as a language structure.
$a='Mike\'s'; //Mike'sMike\'s
$b="Mike\"s"; //Mike"sMike\"s
$c="something $b"; //$b
if you want to put it in the js code, you can use the addslashes
function to deal with it. Please check the documentation for details.