is not defined."-6ren"> is not defined."-我将一个完全可用的网页复制到我网站上的一个新文件中,但是在编写了一个新函数并删除了一些 HTML 代码后,当您按下两个按钮之一时,JS 函数将不会执行。它只是抛出错误“未定义 BeginSort。” -6ren">
gpt4 book ai didi

javascript - 网页抛出错误 " is not defined."

转载 作者:行者123 更新时间:2023-11-30 17:00:10 24 4
gpt4 key购买 nike

我将一个完全可用的网页复制到我网站上的一个新文件中,但是在编写了一个新函数并删除了一些 HTML 代码后,当您按下两个按钮之一时,JS 函数将不会执行。它只是抛出错误“未定义 BeginSort。”

该网页应该用于对文本行进行排序。

在这里,有一些代码:

<html>
<head>
<title>Text sorting</title>

<style type="text/css">
@font-face { font-family: Candles; src: url('../font/Candles.ttf'), url('../font/Candles.eot'); }

.style1
{
font-family: Candles;
font-size: 44pt;
color: #FFFFFF;
}
.style2
{
font-family: Candles;
font-size: 24pt;
color: #FFFFFF;

}
.style3
{
font-family: Candles;
font-size: 24pt;
color: #FFFFFF;
}
.textareastyle
{
width: 450px;
height: 310px;
}
html
{
background: url('../backgrounds/YellowFadingBackground.jpg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
</style>

<script type="text/javascript">
function BeginSort(descend) {
var textlines = [""];
textlines = document.getElementById("TextBox1").value.replace(/(?:\r\n|\r|\n)/g, "\n").split("\n");
var sortedlist = "";
switch(descend) {
case 0:
textlines.sort(function(a, b){return a-b});
break;

case 1:
textlines.sort(function(a, b){return b-a});
break:
}

for(var i=0; i<textlines.length; i++) {
if(i < (textlines.length - 1)) { sortedlist += textlines + "\n"; } else { sortedlist += textlines; }
}

document.getElementById("TextBox1").value = sortedlist;
}
</script>
</head>

<body>
<p class="style1" align="center">Text sorter<br>
<span class="style2">Sort the text lines in ascending or descending order!</span>
</p>
<p align="center" style="color: #FFFFFF; font-size: 16pt">
Text to sort:<br>
<textarea class="textareastyle" id="TextBox1"></textarea>
</p>
<p class="style2" align="center">
<input type="button" value="Ascending" onclick="javascript:BeginSort(0);">&nbsp;<input type="button" value="Descending" onclick="javascript:BeginSort(1);">
</p>
</body>
</html>

最佳答案

您的脚本第 54 行有一个错误,缺少一个“;” (你有一个“:”代替)。

更正代码并测试(我没有错误):

<html>
<head>
<title>Text sorting</title>

<style type="text/css">
@font-face { font-family: Candles; src: url('../font/Candles.ttf'), url('../font/Candles.eot'); }

.style1
{
font-family: Candles;
font-size: 44pt;
color: #FFFFFF;
}
.style2
{
font-family: Candles;
font-size: 24pt;
color: #FFFFFF;

}
.style3
{
font-family: Candles;
font-size: 24pt;
color: #FFFFFF;
}
.textareastyle
{
width: 450px;
height: 310px;
}
html
{
background: url('../backgrounds/YellowFadingBackground.jpg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
</style>

<script type="text/javascript">
function BeginSort(descend) {
var textlines = [""];
textlines = document.getElementById("TextBox1").value.replace(/(?:\r\n|\r|\n)/g, "\n").split("\n");
var sortedlist = "";
switch(descend) {
case 0:
textlines.sort(function(a, b){return a-b});
break;

case 1:
textlines.sort(function(a, b){return b-a});
break;
}

for(var i=0; i<textlines.length; i++) {
if(i < (textlines.length - 1)) { sortedlist += textlines + "\n"; } else { sortedlist += textlines; }
}

document.getElementById("TextBox1").value = sortedlist;
}
</script>
</head>

<body>
<p class="style1" align="center">Text sorter<br>
<span class="style2">Sort the text lines in ascending or descending order!</span>
</p>
<p align="center" style="color: #FFFFFF; font-size: 16pt">
Text to sort:<br>
<textarea class="textareastyle" id="TextBox1"></textarea>
</p>
<p class="style2" align="center">
<input type="button" value="Ascending" onclick="javascript:BeginSort(0);">&nbsp;<input type="button" value="Descending" onclick="javascript:BeginSort(1);">
</p>
</body>
</html>

关于javascript - 网页抛出错误 "<function> is not defined.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29039094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com