gpt4 book ai didi

javascript - JS 引用字符被剥离

转载 作者:行者123 更新时间:2023-11-30 05:51:20 24 4
gpt4 key购买 nike

我正在尝试使用 JS 提取媒体查询内容 css 属性。但是引号似乎被删除了。请参阅以下内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript">

$(window).load(function() {

for(var i = 0; i < document.styleSheets.length; i++){

for(var j = 0; j < document.styleSheets[i].cssRules.length; j++){

for(var k = 0; k < document.styleSheets[i].cssRules[j].cssRules.length; k++){

var css_text = document.styleSheets[i].cssRules[j].cssRules[k].cssText;

console.log('css_text');
console.log(css_text);

}

}

}

});

</script>
<style type="text/css">

@media screen and (max-width: 960px) {

body:after{
content: "hello";
}

}

</style>
</head>
<body style="margin: 0px;">

some stuff here...

</body>
</html>

以上返回:

css_text
body::after { content: hello; }

我希望得到的返回:

css_text
body::after { content: "hello"; }

我使用的是 Google Chrome 浏览器。

有什么办法可以保留原来围绕着 hello 的引号吗?

最佳答案

您需要转义引号。以下链接是一种方法。 http://magp.ie/2011/01/20/addslashes-and-stripslashes-in-javascript/

所以你会写
console.log(addslashes(css_text));

让我知道它是否有效:)

关于javascript - JS 引用字符被剥离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14683913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com