gpt4 book ai didi

将括号中的文本替换为标签的 Javascript,不适用于 <style>

转载 作者:行者123 更新时间:2023-11-28 15:23:15 25 4
gpt4 key购买 nike

我有一段 jquery,它取代了方括号 {}<> body 内部:

JQuery:

window.onload=function() {
$("body").children().html(function (_, html) {
return html
.replace(/\{/g,"<")
.replace(/b\@/g,"strong")
.replace(/\!/g,"/")
.replace(/\}/g,">");
});
}

HTML

<!DOCTYPE HTML>
<html>

<head>
<script src="jquery.js" type="text/javascript"></script>
<script src="tags.js" type="text/javascript"></script>
<meta charset="utf-8">
<title>Something</title>
</head>
<body>
<w>
<style>
.red {
color: red;
}
</style>
{style}
body {
background-color:red;
}
{!style}

{b@ class="red"}
Some Bold Text
{!b@}

</w>
</body>
</html>

但是当我尝试输入 {style}标签替代,样式标签有效,但实际上没有呈现 CSS。我也尝试应用 color:red.red {b@} 的普通样式标签中它不起作用,但如果我内联应用样式,它就会起作用。

问题是:有没有办法让 CSS 在样式标签内正确呈现?

最佳答案

你非常接近。您只需要在代码末尾添加这些行:

$('style').appendTo('head').text(function() {
return $(this).text().replace(/\</g,'{').replace(/\>/g,'}');
});

这会将 style 元素移动到 head,并撤消您之前代码中对这些元素的文本替换。

片段:

window.onload=function() {
$("body").children().html(function (_, html) {
return html
.replace(/\{/g,"<")
.replace(/b\@/g,"strong")
.replace(/\!/g,"/")
.replace(/\}/g,">");
});

$('style').appendTo('head').text(function() {
return $(this).text().replace(/\</g,'{').replace(/\>/g,'}');
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<w>
<style>
.red {
color: red;
}
</style>
{style}
body {
background-color:yellow;
}
{!style}

{b@ class="red"}
Some Bold Text
{!b@}

</w>
</body>

关于将括号中的文本替换为标签的 Javascript,不适用于 &lt;style&gt;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30676718/

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