【问题标题】:markedjs table has no styles标记的js表没有样式
【发布时间】:2018-09-03 03:24:07
【问题描述】:

我在我的网站上使用markedjs markdown 解析器,但表格语法的样式不正确(没有边框和条纹)。我的代码:

<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Marked in the browser</title>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
</head>
<body>
<div id="content"></div>
<script>

    document.getElementById('content').innerHTML =
        marked(`
First Header | Second Header
------------ | -------------
Content Cell | Content Cell
Content Cell | Content Cell
        `);
</script>
</body>
</html>

【问题讨论】:

标签: javascript css html-table javascript-marked


【解决方案1】:

这不是 marked 问题,而是 CSS 问题。由于您没有为表格指定任何样式,因此您没有得到任何样式:)。

编辑Here 是一个使用带有标记js 的Bootstrap 的示例。

非引导示例:

<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Marked in the browser</title>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <style>
        table { border-collapse: collapse; }
        tr { border-bottom: solid 1px black; }
        tr:nth-child(even) {background-color: #f2f2f2;}
    </style>
</head>
<body>
<div id="content"></div>
<script>

    document.getElementById('content').innerHTML =
        marked(`
First Header | Second Header
------------ | -------------
Content Cell | Content Cell
Content Cell | Content Cell
        `);
</script>
</body>
</html>
  • table { border-collapse: collapse; } 启用每tr 边框,每this answer。
  • tr { ... } 为您提供行之间的线条。
  • tr:nth-child(even) { ... } 为您提供阴影。

【讨论】:

  • 谢谢。我可以将引导程序与markedjs一起使用吗?每次将markdown转换为html后,我是否需要将引导程序类(如“table-striped”)动态添加到表格标签中?
  • 编辑重新。引导程序。至于添加引导类,我建议提出一个单独的问题,因为这可能更普遍适用。基于this和this,我认为你需要编写一个自定义渲染器。
  • 是的,绝对的。感谢您的帮助@cxw。
猜你喜欢
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多