首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >HighlightJS中出现的部分CSS样式

HighlightJS中出现的部分CSS样式
EN

Stack Overflow用户
提问于 2018-06-26 04:26:04
回答 1查看 364关注 0票数 2

您好,我正在尝试通过Ajax调用使用highlight.js :它从PHP脚本中提取数据。填充了code元素,但它只设置了背景和字体颜色的样式(在devtools中进行了验证),语法突出显示不会发生而不是。我已经在我的PHP脚本中使用htmlspecialchars清理了这些文件。通过直接在元素中键入代码,我确实获得了正确的行为。我的HTML代码:

代码语言:javascript
复制
<!doctype html>
<html>
    <head>
         <meta charset="utf-8">
         <title>hljs &amp; PHP Proxy</title>
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
         <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/atelier-forest-dark.min.css">
         <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>    
    </head>
    <body>
        <pre>
            <code id="code" class="xml"></code>
        </pre>
        <script type="text/javascript" src="js/mjs-0000B.js"></script>
    </body>
</html>

我的Javascript:

代码语言:javascript
复制
var xhr = new XMLHttpRequest()
var target = document.getElementById('code')

xhr.onload = function(){
    if(xhr.status === 200)
        target.textContent = xhr.responseText
        console.log("Ajax request completed")
}

xhr.open('GET','https://localhost/proxy.php',true)
xhr.send(null)

window.addEventListener("load", function(event) {
    console.log("Window resources loaded");
    window.setTimeout(function(){
        hljs.highlightBlock(target)
    }, 50)
});

PHP:这是废话,但我能让它与CORS一起工作的唯一方法是:

代码语言:javascript
复制
<?php

    $ch = curl_init();
    // set url
    curl_setopt($ch, CURLOPT_URL, "localhost/hljs-test.html");

    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    // $output contains the output string
    $output = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);
    echo htmlspecialchars($output);
?>

我已经看过了这里几乎所有的问题,但还没有找到解决方案。到目前为止,HTML和JSON数据的结果都是一样的--我被难住了。谢谢。

编辑:

以下是从target.textContent请求的输出:

代码语言:javascript
复制
&lt;!doctype html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;header&gt; &lt;meta charset=&quot;utf-8&quot;&gt; &lt;script src=&quot;js/0008.js&quot;&gt;&lt;/script&gt; &lt;/header&gt; &lt;body&gt;&lt;/body&gt; &lt;/html&gt;
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51031335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档