这段代码中有什么错误? [英] what is mistake in this code ?

查看:120
本文介绍了这段代码中有什么错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码有什么错误......点击按钮后没有任何反复发生..





 <   html  >  
< head >
< script >

  function  change()
{
var t = document .getElementById( text1 中);
if (t.value!=
windows.alert( 您输入了: + t。值);
else
windows.alert( 你会输入一些文字吗);
}

 <  < span class =code-leadattribute> / script  >  
< / head >
< body >
< 输入 type = text id = text1 >
< 输入 类型 = 按钮 名称 = b1 value = click me onclick = change(); >
< / body >
< / html >

解决方案

功能变更(){
var t = document.getElementById( text1 中);
if (t。 value !=
alert( 您输入了: + t。 value );
else
alert( 你会输入一些文字);
}





你所拥有的甚至不是有效的javacript。你应该得到一本关于js的书,至少要学习基础知识,因为这不是学习不断要求人们修复你的基本错误的好方法。


这个bug是 lexical :在中你会输入一些文字缺少右引号

这样的错误不能甚至通过处理异常直接捕获。在我的文章的这一章中解释了捕获它们作为例外的方法: JavaScript Calculator, 5。处理词汇错误



此外,您的代码几乎没有实际意义:如果输入,则认为输入为空。如何输入真空字符串,零长度?两个或多个空白字符怎么样?



为了改善这一点,你可以使用字符串函数 .trim()

https:// developer。 mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim [ ^ ]。



-SA

What is the mistake in this code... upon clicking the button nothing happens..


<html>
<head>
<script>

function change()
{
    var t= document.getElementById("text1");
    if (t.value!=" ")
        windows.alert ("You entered: "+t.value);
    else
        windows.alert("would you enter some text);
}

</script>
</head>
<body>
<input type= "text" id="text1">
<input type ="button" name = "b1" value = "click me" onclick ="change();">
</body>
</html>

解决方案

function change() {
        var t = document.getElementById("text1");
        if (t.value != "")
            alert("You entered: " + t.value);
        else
            alert("would you enter some text");
    }



what you had wasn't even valid javacript. You should get a book on js and at least learn the basics as it's not a good way to learn to constantly ask people to fix your basic errors.


The bug is lexical: in "would you enter some text", right quotation mark is missing.
Such bugs cannot be caught directly even by handling exceptions. The way to catch them as exception is explained in this chapter of my article: JavaScript Calculator, 5. Handling Lexical Errors.

Also, your code makes little to no practical sense: you consider input as "empty" only if " " is entered. How about entering "real empty" string, zero-length? how about two or more blank characters?

To improve this, you could use the string function .trim():
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim[^].

—SA


这篇关于这段代码中有什么错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆