在Javascript中处理定义的函数时,“函数未定义”错误

时间:2015-06-21 01:04:11

标签: javascript function focus getelementbyid

我试图在从JavaScript中休息后使用函数(因为它让我对它的语法感到非常悲伤)并且它再次决定再次粗暴地对待我,忽略了我的功能。

<script type="text/javascript">
channel = 1
channel_array = ["welcome_mat.html", "http://www.youtube.com/user/1americanews"];
function Oooh(e){
	var unicode=e.keyCode? e.keyCode : e.charCode
	alert(unicode);
	if (unicode == 38);{
		alert("You hit the up button.");
		if (channel == 65);{
			channel = 1;
			document.getElementById("Frame").src = channel_array[channel]
		}
		else{
			channel = channel + 1;
			document.getElementById("Frame").src = channel_array[channel]
		}
	}
}
</script>
<input id="text2" type="text" size="2" maxlength="1" onkeyup="Oooh(event); this.select()" />
<script type="text/javascript">
document.getElementById("Frame").src="http://www.youtube.com/user/1americanews";
document.getElementById("text2").focus();
</script>

3 个答案:

答案 0 :(得分:2)

在你的第一个if语句之后有一个半冒号

替换

if (channel == 65);{

if (channel == 65){

答案 1 :(得分:1)

您提到您的JavaScript语法有问题,而您的代码就是这样。

更正后的版本为:

<script type="text/javascript">
    var channel = 1;
    var channel_array = ["welcome_mat.html", "http://www.youtube.com/user/1americanews"];
    function Oooh(e) {
        var unicode=e.keyCode ? e.keyCode : e.charCode;
        alert(unicode);
        if (unicode == 38) {
            alert("You hit the up button.");
            if (channel == 65) {
                channel = 1;
                document.getElementById("Frame").src = channel_array[channel];
            }
            else {
                channel = channel + 1;
                document.getElementById("Frame").src = channel_array[channel];
            }
        }
    }
</script>
<input id="text2" type="text" size="2" maxlength="1" onkeyup="Oooh(event); this.select()">
<script type="text/javascript">
    document.getElementById("Frame").src = "http://www.youtube.com/user/1americanews";
    document.getElementById("text2").focus();
</script>

答案 2 :(得分:1)

您的脚本中存在一些错误,这些错误是由丢失和/或无效的令牌/分号引起的。

它应该是这样的:

function Oooh(e) {
    var unicode = e.keyCode ? e.keyCode : e.charCode;
    alert(unicode);
    if (unicode === 38) {
        alert("You hit the up button.");
        if (channel === 65) {
            channel = 1;
            document.getElementById("Frame").src = channel_array[channel];
        } else {
            channel = channel + 1;
            document.getElementById("Frame").src = channel_array[channel];
        }
    }
}

主要问题是;在你的if语句之后。

请注意: 在适当的行的末尾使用分号是JS中的良好编码风格。 使用===而不是==来确保类型安全比较。 尝试将JS代码放在外部文件中。