【发布时间】:2019-10-27 23:22:27
【问题描述】:
我在运行时遇到此错误。我不知道为什么显示 myFunction() 未定义。点击按钮时函数的加载方式是否有问题。
到目前为止,这是我的代码。
缩进.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml"
xmlns:h = "http://java.sun.com/jsf/html"
xmlns:f = "http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<style type="text/css">
.code-str { color: #080;}
.code-elem { color: #f00;}
.code-comment { color: #00f;}
</style>
</h:head>
<h:body>
<h:form>
<p:inputTextarea rows="15" cols="80" id="text1"></p:inputTextarea>
<br/>
<p:commandButton type="button" value = "submit" action="indentation" onclick="myFunction()"></p:commandButton>
<div id="demo"></div>
</h:form>
</h:body>
<script type="text/javascript">
const keywords = {
IF: {style: "code-elem", indent: 4},
ENDIF: {style: "code-elem", indent: -4},
IFLISTING: {style: "code-str", indent: 4},
ENDIFLISTING: {style: "code-str", indent: -4},
VAR: {style: "code-comment", indent: 0},
LISTING: {style: "code-comment", indent: 0}
};
window.onload = function myFunction() {
let indent = 0;
document.getElementById("demo").innerHTML = document.getElementById("text1").value.split(/[\r\n]+/).map(line => {
const oldIndent = indent;
line = line.trim().replace(/###([A-Z]+)(.*?)###/g, (m, keyword, arg) => {
const param = keywords[keyword];
if (!param)
return m;
indent += param.indent;
return `<span class="${param.style}">${m}</span>`;
});
return " ".repeat(Math.min(indent, oldIndent)) + line;
}).join("<br/>");
}
</script>
</html>
请问,谁能帮我找到这里的问题? 按下按钮后,它应该为代码提供一些颜色。
【问题讨论】:
-
我已回滚您的编辑,将答案折叠到问题中。这不是我们在 SO 上做事的方式。问题仍然是一个问题,而答案则回答了它。问题不应成为移动目标。
-
好的。谢谢。 :)
标签: javascript html jsf