gpt4 book ai didi

javascript - 脚本产生我找不到的错误

转载 作者:行者123 更新时间:2023-11-30 07:03:13 26 4
gpt4 key购买 nike

最终的目的是返回一个显示下面这句话的页​​面

Once upon a time there was a GENDER named NAME
who had a PET named PET NAME.

<SCRIPT language="JavaScript">

var heroGender, heroName, petType, petName;

heroGender = window.prompt('Is the hero female or male? Enter F or M', 'F');

heroName = window.prompt('What is the hero\'s name?','');

petType = window.prompt('What type of pet does the hero have?','');

petName = window.prompt('What is name of the pet?','');

document.write('Once upon a time there was a ');

if (heroGender == ('F'))
{
document.write('girl');
}
else
{
document.write('boy');
}

document.write('Once upon a time there was a ' + heroGender + ' named ' + heroName '.''<BR>' +
heroName + ' had a ' + petType + ' called ' + petName + '.');

</SCRIPT>

最佳答案

在您等待书籍的同时!

学习 Mozilla Developer Centre !

一个例子:

Example

固定版本

document.write(
'Once upon a time there was a ' + heroGender +
' named ' + heroName + '.' + '<BR>' + heroName +
' had a ' + petType + ' called ' + petName + '.');

破解版

您缺少 + 符号。

document.write(
'Once upon a time there was a ' + heroGender +
' named ' + heroName /* + */ '.' /* + */ '<BR>' + heroName +
' had a ' + petType + ' called ' + petName + '.');

尝试使用

  • Firefox 和 Firebug及其控制台选项卡(按 F12)
  • IE9 和内置控制台(按 F12。确保控制台已打开,然后刷新)。
  • Chrome 和控制台中的构建(按 Ctrl+Shift+J)

观察任何错误信息。

如果这样太费力,那就试试

window.onerror = function(e) {
alert(e.message);
}

这是一个“改进的”更符合标准的版本。 Example Link!

HTML:

<label> Your Hero's gender </label><input id="heroGender"/><br/>
<label> Your Hero's name </label><input id="heroName"/><br/>
<label> Your Hero's pet type </label><input id="petType"/><br/>
<label> Your Hero's pet name </label><input id="petName"/><br/>
<button> Make me a hero! </button>
<div id="output"></div>

JavaScript:

// make your hero when you press the button
document.getElementsByTagName("button")[0].addEventListener("click", function() {
// get all the values from the text boxes
var gender = document.getElementById("heroGender").value,
name = document.getElementById("heroName").value,
petType = document.getElementById("petType").value,
petName = document.getElementById("petName").value;

// set the text on your output.
document.getElementById("output").textContent =
"Once upon a time there was a " + gender +
" named " + name + ". " + name + " had a pet " +
petType + " called " + petName;


}, false);

上面的代码将在 IE8 或更低版本上中断:(。让 JavaScript 跨浏览器工作是一件很痛苦的事。

所以我可以在

阅读浏览器的文档

但这些都不容易阅读或浏览。 visibone BrowserBook 是一个很好的跨浏览器脚本可视化指南。 .

它将显示跨浏览器支持(红色是 firefox,蓝色是 IE):

enter image description here

给它几个月的时间,你就会知道如何舒适地使用所有这些东西。

关于javascript - 脚本产生我找不到的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6036480/

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