fiddle -http://jsfiddle.net/Mukdf/
今天我决定将 html 的 css attr 镜像到 Codemirror,看看它是否可以渲染出干净的 CSS,而不是全部渲染在一行上。
这是正在镜像到 Codemirror 的 CSS...
display: inherit; position: absolute; top: 20px; left: 19px; margin: 10px; width: 340px; height: 234px; padding: 1em; font-style: normal; font-variant: normal; font-weight: normal; font-size: 14px; font-family: arial; line-height: normal; color: rgb(255, 255, 255); border: 0px dashed rgb(0, 255, 255); border-top-left-radius: 0em; border-top-right-radius: 0em; border-bottom-right-radius: 0em; border-bottom-left-radius: 0em; background-color: rgb(0, 34, 102); text-shadow: rgb(255, 255, 255) 0px 0px 8px; overflow: visible; z-index: 0;
我正在尝试让它像这样显示......
display: inherit;
position: absolute;
top: 20px;
left: 19px;
margin: 10px;
width: 340px;
height: 234px;
padding: 1em;
font-style: normal;
font-variant: normal;
font-weight: normal;
font-size: 14px;
font-family: arial;
line-height: normal;
color: rgb(255, 255, 255);
border: 0px dashed rgb(0, 255, 255);
border-top-left-radius: 0em;
border-top-right-radius: 0em;
border-bottom-right-radius: 0em;
border-bottom-left-radius: 0em;
background-color: rgb(0, 34, 102);
text-shadow: rgb(255, 255, 255) 0px 0px 8px;
overflow: visible;
z-index: 0;
我知道这是可能的,但我只是不知道该怎么做。我想找到 ;
并将其替换为相同的字符,但在末尾添加换行符。
非常感谢任何帮助。
这是我的 JQuery/JS...
$('.div-style').val($('.box').attr('style'));
$('.code').each(function() {
CodeMirror(this, {
value: $('.div-style').val(),
mode: 'text/css',
lineNumbers: true,
lineWrapping: true,
readOnly: true
});
});
最佳答案
将所有分号更改为分号后跟换行符的全局替换如下:
$('.div-style').val().replace(/;/g,";\n")
尽管您会注意到,除了第一行之外,所有行的开头都会留下一个空格,因为您的输入在每个分号后面都有一个空格: http://jsfiddle.net/Mukdf/1/
也许是这样的:
$('.div-style').val().replace(/;\s?/g,";\n")
演示:http://jsfiddle.net/Mukdf/2/
或者 /;\s*/g
如果您想在每个分号后允许任意数量的空格(包括零)。
关于javascript - 将 HTML 的 CSS Attr 镜像到 Codemirror(插入新行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21359104/