css JS:当我打开html文件时,文本不会自动着色,启动函数时出现问题

tpxzln5u  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(126)

我有问题的JavaScript脚本颜色的文本.它工作正常,但文本只有颜色,如果你点击面板上,并按下“空间”按钮在PC键盘上.如果你按任何其他键(甚至回车),文本不着色.


的数据
我想该脚本自动激活,只要我打开的html文件,使文本自动着色,只要我打开文件。
在html中,我尝试同时使用onchangeoninput函数。而在js文件中,我在创建changeColor()函数后调用它,目的是在打开文件时自动执行它,但它不起作用。抱歉的问题,但我是新来的。我如何解决它?

index.html

  1. <div id="editor" contenteditable="true" oninput="showPreview();" onchange="changeColor();">&lt;h1>This is a Heading&lt;/h1>
  2. &lt;p>This is a paragraph.&lt;/p>
  3. </div>
  4. <h3>PREVIEW</h3>
  5. <div class="preview-area">
  6. <iframe id="preview-window"></iframe>
  7. </div>

字符串

style.css

  1. #editor {
  2. width: 400px;
  3. height: 100px;
  4. padding: 10px;
  5. background-color: #444;
  6. color: white;
  7. font-size: 14px;
  8. font-family: monospace;
  9. white-space: pre;
  10. }
  11. .statement {
  12. color: orange;
  13. }

JavaScript.js

  1. // CHANGE COLOR TEXT
  2. function changeColor() {
  3. var keywords = ["DIV", "DIV", "H1", "H1", "P", "P", "HELLO", "<", ">", "/"];
  4. // Keyup event
  5. document.querySelector("#editor").addEventListener("keyup", (e) => {
  6. // Space key pressed
  7. if (e.keyCode == 32) {
  8. var newHTML = "";
  9. // Loop through words
  10. str = e.target.innerText;
  11. (chunks = str
  12. .split(new RegExp(keywords.map((w) => `(${w})`).join("|"), "i"))
  13. .filter(Boolean)),
  14. (markup = chunks.reduce((acc, chunk) => {
  15. acc += keywords.includes(chunk.toUpperCase())
  16. ? `<span class="statement">${chunk}</span>`
  17. : `<span class='other'>${chunk}</span>`;
  18. return acc;
  19. }, ""));
  20. e.target.innerHTML = markup;
  21. // Set cursor postion to end of text
  22. // document.querySelector('#editor').focus()
  23. var child = e.target.children;
  24. var range = document.createRange();
  25. var sel = window.getSelection();
  26. range.setStart(child[child.length - 1], 1);
  27. range.collapse(true);
  28. sel.removeAllRanges();
  29. sel.addRange(range);
  30. this.focus();
  31. }
  32. })
  33. }
  34. changeColor()
  35. //PREVIEW
  36. function showPreview() {
  37. var editor = document.getElementById("editor").innerText;
  38. // var cssCode =
  39. // "<style>" + document.getElementById("cssCode").value + "</style>";
  40. // var jsCode =
  41. // "<scri" + "pt>" + document.getElementById("jsCode").value + "</scri" + "pt>";
  42. var frame = document.getElementById("preview-window").contentWindow.document;
  43. document.getElementById("preview-window").srcdoc = editor;
  44. frame.open();
  45. //frame.write(htmlCode + cssCode + jsCode);
  46. frame.write(editor);
  47. frame.close();
  48. }
  49. showPreview()

gopyfrb3

gopyfrb31#

您可以考虑将changeColor()的着色部分重构为另一个函数,并在初始加载时立即在JavaScript中调用它:

  1. function applyColoring(element) {
  2. var keywords = ["DIV", "DIV", "H1", "H1", "P", "P", "HELLO", "<", ">", "/"];
  3. var newHTML = "";
  4. // Loop through words
  5. str = element.innerText;
  6. (chunks = str
  7. .split(new RegExp(keywords.map((w) => `(${w})`).join("|"), "i"))
  8. .filter(Boolean)),
  9. (markup = chunks.reduce((acc, chunk) => {
  10. acc += keywords.includes(chunk.toUpperCase())
  11. ? `<span class="statement">${chunk}</span>`
  12. : `<span class='other'>${chunk}</span>`;
  13. return acc;
  14. }, ""));
  15. element.innerHTML = markup;
  16. }
  17. // CHANGE COLOR TEXT
  18. function changeColor() {
  19. // Keyup event
  20. document.querySelector("#editor").addEventListener("keyup", (e) => {
  21. // Space key pressed
  22. if (e.keyCode == 32) {
  23. applyColoring(e.target);
  24. // Set cursor postion to end of text
  25. // document.querySelector('#editor').focus()
  26. var child = e.target.children;
  27. var range = document.createRange();
  28. var sel = window.getSelection();
  29. range.setStart(child[child.length - 1], 1);
  30. range.collapse(true);
  31. sel.removeAllRanges();
  32. sel.addRange(range);
  33. this.focus();
  34. }
  35. });
  36. }
  37. changeColor();
  38. applyColoring(document.getElementById('editor'));
  39. //PREVIEW
  40. function showPreview() {
  41. var editor = document.getElementById("editor").innerText;
  42. // var cssCode =
  43. // "<style>" + document.getElementById("cssCode").value + "</style>";
  44. // var jsCode =
  45. // "<scri" + "pt>" + document.getElementById("jsCode").value + "</scri" + "pt>";
  46. var frame = document.getElementById("preview-window").contentWindow.document;
  47. document.getElementById("preview-window").srcdoc = editor;
  48. frame.open();
  49. //frame.write(htmlCode + cssCode + jsCode);
  50. frame.write(editor);
  51. frame.close();
  52. }
  53. showPreview();
  1. #editor {
  2. width: 400px;
  3. height: 100px;
  4. padding: 10px;
  5. background-color: #444;
  6. color: white;
  7. font-size: 14px;
  8. font-family: monospace;
  9. white-space: pre;
  10. }
  11. .statement {
  12. color: orange;
  13. }
  1. <div
  2. id="editor"
  3. contenteditable="true"
  4. oninput="showPreview();"
  5. onchange="changeColor();"
  6. >&lt;h1>This is a Heading&lt;/h1>
  7. &lt;p>This is a paragraph.&lt;/p></div>
  8. <h3>PREVIEW</h3>
  9. <div class="preview-area">
  10. <iframe id="preview-window"></iframe>
  11. </div>
展开查看全部

相关问题