将括号内的文本替换为标记的Javascript不';t与<;风格>;
我有一段jquery,它将主体内的括号将括号内的文本替换为标记的Javascript不';t与<;风格>;,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一段jquery,它将主体内的括号{}替换为: JQuery: window.onload=function() { $("body").children().html(function (_, html) { return html .replace(/\{/g,"<") .replace(/b\@/g,"strong") .replace(/\!/g,"/") .replace(/\}/g,">"); }); } window.o
{}
替换为
:
JQuery:
window.onload=function() {
$("body").children().html(function (_, html) {
return html
.replace(/\{/g,"<")
.replace(/b\@/g,"strong")
.replace(/\!/g,"/")
.replace(/\}/g,">");
});
}
window.onload=function(){
$(“body”).children().html(函数(\ux,html){
返回html
.替换(/\{/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}
身体{
背景色:红色;
}
{!风格}
{b@class=“red”}
一些粗体字
{!b@}
但是,当我尝试放入{style}
标记子项时,样式标记可以工作,但实际上没有呈现CSS。此外,当我尝试将正常样式标记中的颜色:红色
应用于.red{b@}
时,它不起作用,但如果我内联应用样式,它就可以工作。
问题是:有没有办法使CSS在样式标记中正确呈现?这是因为自从您编写
{style}
以来,
标记还没有被浏览器编译,您无法通过将{}
替换为
来使其工作。
您需要在脚本执行时进行替换,然后加载jquery中的所有代码,并将其附加到非常接近的页面中。您只需在代码末尾添加以下行:
$('style').appendTo('head').text(function() {
return $(this).text().replace(/\</g,'{').replace(/\>/g,'}');
});
瑞德先生{
颜色:红色;
}
{style}
身体{
背景颜色:黄色;
}
{!风格}
{b@class=“red”}
一些粗体字
{!b@}
正如@barmar所说,{}
覆盖了CSS括号,所以我只是将{}
更改为[]
,这样它就不会干扰。这是因为主体{背景色:红色;}
变成body
样式标记不允许在body中,它们必须在头部。@Barmar除非它们在范围内或在模板内。