Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 配置prism.js以识别标记(不带标记)_Javascript_Html_Syntax Highlighting - Fatal编程技术网

Javascript 配置prism.js以识别标记(不带标记)

Javascript 配置prism.js以识别标记(不带标记),javascript,html,syntax-highlighting,Javascript,Html,Syntax Highlighting,js文档说明 Prism强制您使用正确的元素标记代码:。单独用于内联代码,或内部由CMS转换为 有没有一种方法可以让prism.js向标记添加语法高亮显示,如下所示: <script> // Enable the "manual" mode to prevent prism from instantly firing. window.Prism = window.Prism || {}; window.Prism.manual = true; <

js文档说明

Prism强制您使用正确的元素标记代码:。单独用于内联代码,或内部由CMS转换为 有没有一种方法可以让prism.js向标记添加语法高亮显示,如下所示:

<script>
  // Enable the "manual" mode to prevent prism from instantly firing.
  window.Prism = window.Prism || {};
  window.Prism.manual = true;
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/prism.min.js" defer></script>

<script>
  // THE FOLLOWING BLOCK CHANGED:

  jQuery(function() {
    // Wrap the code inside the required <code> tag, when needed:
    jQuery('pre[class*="language-"], pre[class*="lang-"]').each(function() {
      if (1 !== jQuery(this).children('code').length) {
        jQuery(this).wrapInner('<code>');
      }
    });

    // Highlight code, when the page finished loading (using jQuery here)
    Prism.highlightAll()
  });
</script>

也许有一个插件或JS配置告诉prism.JS突出显示那些标签。

我可以做到。这是代码,我不认为你需要语言js我以后怎么做

因此,在我的示例中,我执行以下操作:

示例如下:


我相信您可以找到如何查询选择所有pre标记并循环使用它们的每个格式:

我能够做到。这是代码,我不认为你需要语言js我以后怎么做

因此,在我的示例中,我执行以下操作:

示例如下:

我相信您可以找到如何查询选择所有前置标记并循环使用每个标记的格式:

Solution 1 original Response 下面是我根据以下内容编写的代码:

解决方案1原始答案 下面是我根据以下内容编写的代码:


谢谢正是我想要的。我已经查阅了这个答案中解释的Prism方法的文档,找到了一个简单明了的方法来解决这个问题谢谢!正是我想要的。我查阅了这个答案中解释的Prism方法的文档,找到了一个简单而干净的方法来解决这个问题
<pre class="language-js">
  var cheese = sandwich;
  function(){
    return "hello!";
  }
</pre>
<script>
  window.Prism = window.Prism || {};
  window.Prism.manual = true;
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/prism.min.js"></script>
// The code snippet you want to highlight, as a string
const code = `var data = 1;`;

// Returns a highlighted HTML string
const html = Prism.highlight(code, Prism.languages.javascript, 'javascript');
<script>
  // Get the pre element
  let pre = document.querySelector("pre");
  // Grab the text out of it
  let code = pre.innerText;
  // Highlight it
  let highlighted = Prism.highlight(code, Prism.languages.javascript, 'javascript');
  // Now put that back in, but as HTML
  pre.innerHTML = highlighted
</script>
<script>
  // Enable the "manual" mode to prevent prism from instantly firing.
  window.Prism = window.Prism || {};
  window.Prism.manual = true;
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/prism.min.js" defer></script>

<script>
  // Use the hook "before-highlightall" to register the custom CSS selectors:
  Prism.hooks.add('before-highlightall', function (env) {
    env.selector += ', pre[class*="language-"], pre[class*="lang-"]';
  });

  // Highlight code, when the page finished loading (using jQuery here)
  jQuery(Prism.highlightAll);
</script>
<script>
  // Enable the "manual" mode to prevent prism from instantly firing.
  window.Prism = window.Prism || {};
  window.Prism.manual = true;
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/prism.min.js" defer></script>

<script>
  // THE FOLLOWING BLOCK CHANGED:

  jQuery(function() {
    // Wrap the code inside the required <code> tag, when needed:
    jQuery('pre[class*="language-"], pre[class*="lang-"]').each(function() {
      if (1 !== jQuery(this).children('code').length) {
        jQuery(this).wrapInner('<code>');
      }
    });

    // Highlight code, when the page finished loading (using jQuery here)
    Prism.highlightAll()
  });
</script>