睿智的海鸥 2024-11-02 12:51 采纳率: 50%
浏览 27
已结题

HTML在线运行和直接打开显示不同的网页问题

使用HTML在线运行(https://www.runoob.com/runcode runoob教程在线代码运行工具)和直接使用Google Chrome浏览器打开HTML文件效果不一样

主要差异在于在线代码运行工具在以下几处中与直接打开文件显示不同

img

img

效果图

https://www.runoob.com/runcode runoob教程在线代码运行工具 的效果

img

img

img

直接打开文件的效果

img

我的代码如下 (由于csdn必须要代码在代码块内,所以HTMK代码中的部分MARKDOWN内容的代码块语法前我增加了转义字符 \ 运行时请去除,谢谢 )

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Marked.js Example with Highlight.js</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
        }
        #output {
            white-space: pre-wrap; /* Keep line breaks */
            border: 1px solid #ccc;
            padding: 10px;
            margin-top: 20px;
        }
        pre {
            background-color: #f8f8f8;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            overflow: auto;
        }
        code {
            background-color: #f8f8f8;
            padding: 2px 4px;
            border-radius: 3px;
        }
    </style>
</head>
<body>
    <textarea id="markdown-input" style="width: 100%; height: 150px;">
# Markdown Input

Here is some **markdown** text with *emphasis*.
- This is a list item.
- Here's another one.

And a `code` snippet:

\```javascript
function sayHello() {
  console.log('Hello!');
}
\```

\```cpp
#include<iostream>
using namespace std;

int main(){
    cout<<"HelloWorld!";
    return 0;
}
\```
   

    </textarea>
    <div id="output"></div>

    <!-- Load marked and highlight libraries from CDN -->
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/highlight.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/styles/github.min.css">

    <script>
        // Get the textarea and output div
        const markdownInput = document.getElementById('markdown-input');
        const outputDiv = document.getElementById('output');

        // Convert markdown to HTML and update the output div
        function updateOutput() {
            const markdownSource = markdownInput.value;
            const html = marked.parse(markdownSource);
            outputDiv.innerHTML = html;
            // Highlight the code blocks after rendering them with marked.js
            hljs.highlightAll();
        }

        // Listen for changes in the textarea and update the output when they occur
        markdownInput.addEventListener('input', updateOutput);

        // Initialize the output with the initial markdown content
        updateOutput();
    </script>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • caperxi 2024-11-05 09:11
    关注

    应该是样式文件的缺失,你可以在菜鸟教程里面 F12 看看都引入了什么文件,特别是 .css 和 .js ,然后 copy 过来试试

    如有帮助,请采纳!!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月29日
  • 已采纳回答 11月21日
  • 创建了问题 11月2日