Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.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 PRISMJ未突出显示动态内容_Javascript_Syntax Highlighting_Prismjs - Fatal编程技术网

Javascript PRISMJ未突出显示动态内容

Javascript PRISMJ未突出显示动态内容,javascript,syntax-highlighting,prismjs,Javascript,Syntax Highlighting,Prismjs,我使用Prismjs进行语法高亮显示,我的方法是使用fetch方法从json文件中获取数据,并使用js动态呈现html。问题是Prismjs是在本地环境中工作,而不是在生产中,顺便说一句,我使用Netlify来主持该网站。我也在使用Prismjs.highlightall()方法,但在托管站点之后,它似乎仍然无法工作。链接到站点: 我的JSON文件 [ { "id": "background", "name": &q

我使用Prismjs进行语法高亮显示,我的方法是使用fetch方法从json文件中获取数据,并使用js动态呈现html。问题是Prismjs是在本地环境中工作,而不是在生产中,顺便说一句,我使用Netlify来主持该网站。我也在使用Prismjs.highlightall()方法,但在托管站点之后,它似乎仍然无法工作。链接到站点:
我的JSON文件

[
  {
    "id": "background",
    "name": "background",
    "longhand": "\n  .element {\n\t background-color: #000;\n\t background-image: url(image.png);\n\t background-repeat: no-repeat;\n\t background-position: top left;\n\t background-attachment: fixed;\n  }",
    "shorthand": "\n  .element {\n\t background: #000 url(img.png)no-repeat top left fixed;\n  }"
  },
  {
    "id": "border",
    "name": "border",
    "longhand": "\n  .element {\n\t border-width: 1px;\n\t border-style: solid;\n\t border-color: black;\n  }",
    "shorthand": "\n  .element {\n\t border: 1px solid black;\n  }"
  },
  {
    "id": "outline",
    "name": "outline",
    "longhand": "\n  .element {\n\t outlie-width: thick;\n\t outline-style: dotted;\n\t outline-color: red;\n  }",
    "shorthand": "\n  .element {\n\t outline: thick dotted red;\n  }"
  },
  {
    "id": "font",
    "name": "font",
    "longhand": "\n  .element {\n\t font-weight: bold;\n\t font-style: italic;\n\t font-variant: small-caps;\n\t font-size: 1em;\n\t line-height: 1.6;\n\t font-family: Arial, Helvetica, sans-serif;\n }",
    "shorthand": "\n  .element {\n\t font: bold italic small-caps 1em/1.6 Arial, Helvetica, sans-serif;\n  }"
  },
  {
    "id": "margin",
    "name": "margin",
    "longhand": "\n  .element {\n\t margin-top: 1em;\n\t margin-right: 1.5em;\n\t margin-bottom: 2em;\n\t margin-left: 2.5em; \n  } \n\n /* or */ \n\n  .element {\n\t margin-top: 1em;\n\t margin-right: .5em;\n\t margin-bottom: 1em;\n\t margin-left: .5em; \n  }",
    "shorthand": "\n  .element {\n\t margin: 1em 1.5em 2em 2.5em;\n  } \n\n /* or */ \n\n  .element {\n\t margin: 1em .5em;\n  }"
  },
  {
    "id": "padding",
    "name": "padding",
    "longhand": "\n  .element {\n\t padding-top: 1em;\n\t padding-right: 1.5em;\n\t paddin-bottom: 2em;\n\t padding-left: 2.5em; \n  } \n\n /* or */ \n\n  .element {\n\t padding-top: 1em;\n\t padding-right: .5em;\n\t padding-bottom: 1em;\n\t padding-left: .5em; \n  }",
    "shorthand": "\n  .element {\n\t padding: 1em 1.5em 2em 2.5em;\n  } \n\n /* or */ \n\n  .element {\n\t padding: 1em .5em; \n  }"
  },
  {
    "id": "list",
    "name": "list",
    "longhand": "\n  .element {\n\t list-style-type: square;\n\t list-style-position: inside;\n\t list-style-image: url(\"image.png\");\n  }",
    "shorthand": "\n  .element {\n\t list: square inside url(\"image.png\");\n  }"
  },
  {
    "id": "animation",
    "name": "animation",
    "longhand": "\n  .element {\n\t animation-name: motion;\n\t animation-duration: 2s;\n\t animation-timing-function: ease-in;\n\t animation-delay: 1s;\n\t animation-direction: alternate;\n\t animation-iteration-count: infinite;;\n\t animation-fill-mode: none;\n\t animation-play-state: running;\n  }",
    "shorthand": "\n  .element {\n\t animation: motion 2s ease-in 1s alternate infinite none running;\n  }"
  },
  {
    "id": "flex",
    "name": "flex",
    "longhand": "\n  .element {\n\t flex-grow: 1;\n\t flex-shrink: 1;\n\t flex-basis: auto;\n  }",
    "shorthand": "\n  .element {\n\t flex: 1 1 auto;\n  }"
  }
]

JavaScript代码:
const root = document.getElementById("root");

fetch("./shorthands.json")
  .then((res) => res.json())
  .then((data) => appendData(data))
  .catch((err) => console.log(err));

function appendData(data) {
  for (let i = 0; i < data.length; i++) {
    const syntaxContainer = document.createElement("div");
    syntaxContainer.classList.add("w-full", "py-5");

    syntaxContainer.innerHTML = `<div class="flex items-center" id="${data[i].id}">
          <img src="./hashtag.svg" alt="hashlink" class="w-8" />
          <h1 class="text-white text-3xl">${data[i].name}</h1>
        </div>
        <div class="mt-5 ml-2 text-white">
          <p class="text-lg">longhand</p>
          <pre id="pre" >
          <code class="language-css">${data[i].longhand}
              </code></pre>
        </div>
        <div class="mt-5 ml-2 text-white">
          <p class="text-lg">shorthand</p>
          <pre><code class="language-css ">${data[i].shorthand}
              </code></pre>
        </div>

        </div>
        `;

    root.appendChild(syntaxContainer);
  }
}

const root=document.getElementById(“根”);
fetch(“./shorthands.json”)
.然后((res)=>res.json())
.然后((数据)=>追加数据(数据))
.catch((err)=>console.log(err));
函数追加数据(数据){
for(设i=0;i手写

${data[i].longhand}

速记

${data[i].速记} `; appendChild(syntaxContainer); } }