javascript - 将 HTML 的 CSS Attr 镜像到 Codemirror(插入新行)

标签 javascript jquery

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/

相关文章:

javascript - Codesandbox 上的意外 token ReactJS 应用程序

php - Drupal 网站的滚动效果

javascript - 如何在鼠标移动事件时更改窗口中心和图像宽度

javascript - 根据复选框 ID 值填充输入字段

javascript - IE 11 - console.log 显示对象属性未定义

javascript - 在PhysicsJS中停止 body 旋转

jQuery 从类中更改单个元素

javascript - .on 单击动态创建的项目

javascript - 如何在不同的函数中使用变量?

jquery - 为什么在添加 Google Map 时两个 DIVS 没有对齐