JavaScript 明明写对了却无法正常工作
JavaScript is not working when clearly it was written correctly
我正在制作一个自动为代码着色的东西。到目前为止,这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Put this into an IFRAME on your website</title>
<style>
body{
padding: 0px;
margin: 0px;
overflow: scroll;
background: #000000;
}
#code{
color: #ffffff;
display: block;
width: 100vw;
padding: 2px;
box-sizing: border-box;
position: absolute;
top: 0px;
left: 0px;
}
</style>
<noscript>
<style>
#code{
animation: flash 1s linear infinite;
}
@keyframes flash{
0%{
background: #ff0000;
color: #ff0000;
}
}
</style>
</noscript>
</head>
<body onload="alert('...');">
<pre id="code">
Insert this page into an IFRAME
?scheme=XXXX&lang=XXXX URL queries:
scheme: The color scheme for the code coloration.
Values: normal
lang: The programming language.
Values: js, html
editable: Whether or not the code can be edited.
Values: true, false
var
</pre>
<script type="text/javascript">
alert("script is running");
try{
var schemes: {
normal: {
js: {
background: "#ffffff",
base: "#000000",
variable: "#3c0029",
fn: "#0000ff",
string: "#0000ff",
number: "#0000ff",
boolean: "#006666",
parameter: "#ffcc00",
system: "#006666",
data: "#008800",
comments: "#006600",
misc: "#224444"
}
}
};
var data = {
scheme: "normal",
lang: "html"
};
var code = document.getElementById("code");
window.onload = function(){
alert(location.search);
if(location.search){
var s = locations.search.substring(1);
var ss = s.split("&");
for(var i = 0; i < ss.length; i++){
var ssn = ss[i].split("=");
uri(ssn[0],ssn[1]);
alert(ssn);
}
alert(s);
alert(ss);
}
setInterval(() => {
setColors();
},1);
}
function uri(name,value){
try{
if(name == "scheme"){
data.scheme = schemes[value];
}
if(name == "lang"){
data.lang = value;
}
if(name == "editable"){
code.contenteditable = value == true;
}
}catch(err){
alert(err);
}
}
function setColors(){
var col = data.scheme[data.lang];
document.body.style.background = col.background;
code.style.color = col.base;
if(data.lang == "js"){
code.innerHTML = code.innerHTML.replace("var","<span style='color: " + col.variable + "'>Var</span>");
}
}
}catch(err){
alert(err);
}
</script>
</body>
</html>
我的问题是 JavaScript 不工作。我试了又试,但是脚本标签中的 alert()
不会触发,只会触发正文事件处理程序中的那个。我测试了一个名为 l()
的函数,其中有一个随机警报,然后在正文的 onclick 属性中调用它,但它只显示元素的事件属性中的脚本有效。
你有一个语法错误
应该是 =
而不是 :
我正在制作一个自动为代码着色的东西。到目前为止,这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Put this into an IFRAME on your website</title>
<style>
body{
padding: 0px;
margin: 0px;
overflow: scroll;
background: #000000;
}
#code{
color: #ffffff;
display: block;
width: 100vw;
padding: 2px;
box-sizing: border-box;
position: absolute;
top: 0px;
left: 0px;
}
</style>
<noscript>
<style>
#code{
animation: flash 1s linear infinite;
}
@keyframes flash{
0%{
background: #ff0000;
color: #ff0000;
}
}
</style>
</noscript>
</head>
<body onload="alert('...');">
<pre id="code">
Insert this page into an IFRAME
?scheme=XXXX&lang=XXXX URL queries:
scheme: The color scheme for the code coloration.
Values: normal
lang: The programming language.
Values: js, html
editable: Whether or not the code can be edited.
Values: true, false
var
</pre>
<script type="text/javascript">
alert("script is running");
try{
var schemes: {
normal: {
js: {
background: "#ffffff",
base: "#000000",
variable: "#3c0029",
fn: "#0000ff",
string: "#0000ff",
number: "#0000ff",
boolean: "#006666",
parameter: "#ffcc00",
system: "#006666",
data: "#008800",
comments: "#006600",
misc: "#224444"
}
}
};
var data = {
scheme: "normal",
lang: "html"
};
var code = document.getElementById("code");
window.onload = function(){
alert(location.search);
if(location.search){
var s = locations.search.substring(1);
var ss = s.split("&");
for(var i = 0; i < ss.length; i++){
var ssn = ss[i].split("=");
uri(ssn[0],ssn[1]);
alert(ssn);
}
alert(s);
alert(ss);
}
setInterval(() => {
setColors();
},1);
}
function uri(name,value){
try{
if(name == "scheme"){
data.scheme = schemes[value];
}
if(name == "lang"){
data.lang = value;
}
if(name == "editable"){
code.contenteditable = value == true;
}
}catch(err){
alert(err);
}
}
function setColors(){
var col = data.scheme[data.lang];
document.body.style.background = col.background;
code.style.color = col.base;
if(data.lang == "js"){
code.innerHTML = code.innerHTML.replace("var","<span style='color: " + col.variable + "'>Var</span>");
}
}
}catch(err){
alert(err);
}
</script>
</body>
</html>
我的问题是 JavaScript 不工作。我试了又试,但是脚本标签中的 alert()
不会触发,只会触发正文事件处理程序中的那个。我测试了一个名为 l()
的函数,其中有一个随机警报,然后在正文的 onclick 属性中调用它,但它只显示元素的事件属性中的脚本有效。
你有一个语法错误
=
而不是 :