未捕获的类型错误:无法读取 null 的属性“toLowerCase”
Uncaught TypeError: Cannot read property 'toLowerCase' of null
这个提示在我更新其他一些javascript之前一直运行良好。我不知道我是怎么搞砸的。此函数在 body tag 中声明为运行"onload"。
function funcPrompt() {
var answer = prompt("Are you a photographer?", "Yes/No");
answer = answer.toLowerCase();
if (answer == "yes") {
alert('Excellent! See our links above and below to see more work and find contact info!');
}
else if(answer == "no") {
alert('That is okay! See our links above and below to learn more!');
}
else if(answer == null || answer == "") {
alert('Please enter an answer.');
funcPrompt();
}
else {
alert('Sorry, that answer is not an option');
funcPrompt();
}
}
现在突然我收到此错误,并且不会出现提示。
不确定为什么你会得到一个空,但如果你想避免空,请使用这个:
answer = (answer?answer:'').toLowerCase();
如果我们单击取消,提示将返回null
并且不能在null
上应用toLowerCase
(会导致异常!
在所有其他条件之前添加一个条件answer===null
,return
以停止执行function
function funcPrompt() {
var answer = prompt("Are you a photographer?", "Yes/No");
if (answer === null || answer === "") {
alert('Please enter an answer.');
funcPrompt();
return;
}
answer = answer.toLowerCase();
if (answer == "yes") {
alert('Excellent! See our links above and below to see more work and find contact info!');
} else if (answer == "no") {
alert('That is okay! See our links above and below to learn more!');
} else {
alert('Sorry, that answer is not an option');
funcPrompt();
}
}
funcPrompt();
在您的情况下,最好使用确认而不是提示
function funcConfirm() {
var answer = confirm("Are you a photographer?");
if (answer === true) {
alert('Excellent! See our links above and below to see more work and find contact info!');
} else {
alert('That is okay! See our links above and below to learn more!');
}
}
funcConfirm();
相关文章:
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- 未捕获的类型错误:无法读取属性'状态'在react中为null
- Javascript Float32数组抛出无法读取属性'0'即使数组定义良好,也为null
- 我该如何解决这个问题?“未捕获的类型错误:无法读取 null 的属性'appendChild'”
- VM5111:1249未捕获类型错误:无法读取属性'x'为null
- 无法读取属性'addEventListener'菜单上为null的
- Catch无法读取null的属性
- select2:无法读取null的属性查询
- 获得'无法读取null的属性样式'
- TinyMCE->无法读取属性'setAttribute'为null
- 未捕获的类型错误:无法读取 null 的属性“removeChild”(使用 Instagram 的 JSONP 响应)
- 未捕获的类型错误:无法读取 null 的属性“toLowerCase”
- JAVASCRIPT - 未捕获的类型错误:无法读取 null 的属性“值”
- Google Map API BackBoneJS 无法读取 null 的属性 'offsetWidth'
- 未捕获的类型错误:无法读取 null 的属性“选项” - 某些代码有效
- javascript无法读取属性'style'为null
- 无法读取属性''为null
- Vaadin组合框无法读取属性'addEventListener'为null
- 无法从 NULL 读取属性“患者”