Home >Web Front-end >Front-end Q&A >What to do if there are double quotes in the html method in jquery

What to do if there are double quotes in the html method in jquery

WBOY
WBOYOriginal
2022-06-02 15:14:142297browse

方法:1、将html()方法中的双引号前添加“\”符号,语法为“元素对象.html("...\"...")”;2、将html()方法中的双引号替换成“"”即可,语法为“元素对象.html("..."...")”。

What to do if there are double quotes in the html method in jquery

本教程操作环境:windows10系统、jquery3.2.1版本、Dell G3电脑。

jquery中html方法中有双引号怎么办

在使用jquery的html()方法时,有时候里面添加的html代码含有一些特殊字符,需要进行转义。

 如果这样直接写的话,在chrome和FF浏览器下,没有问题,但在IE8下会报错。

解决方法就是将javascript里面的"进行转义改成",这样就不报错了。

部分字符转义如下:

  •  &符号:"&", "&"

  • 双引号:"\"", """

  • 小于号:"

  • 单引号:"'", "'"

示例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>123</title>
<script src="js/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$(".p1").html("他说&quot你好&quot");
$(".p2").html("他说\"你好\"");
});
</script>
</head>
<body>
<p class="p1">这是一个段落。</p>
<p class="p2">这是另一个段落。</p>
</body>
</html>

输出结果:

What to do if there are double quotes in the html method in jquery

视频教程推荐:jQuery视频教程

The above is the detailed content of What to do if there are double quotes in the html method in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn