脚本产生我找不到的错误

发布于 2024-11-08 16:39:05 字数 841 浏览 0 评论 0原文

最终目的是返回一个显示以下句子的页面

从前,有一个名为 NAME
的性别,他有一个名为 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>

The ultimate aim is to return a page displaying the following sentence

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

十雾 2024-11-15 16:39:05

在等待书籍时!

Mozilla 开发者中心

示例:

示例

固定版本

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);
}

这是一个“改进的”更符合标准的版本。 示例链接!

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):

在此处输入图像描述

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

While your waiting for the books!

Learn from Mozilla Developer Centre !

An Example:

Example

Fixed version

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

Broken version

You were missing to + symbols.

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

Try using

  • Firefox and Firebug and its console tab (Hit F12)
  • IE9 and the build in console (Hit F12. Make sure the console is open then refresh).
  • Chrome and the build in console (Hit Ctrl+Shift+J)

To watch for any error messages.

If that's too much effort then try

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

Here's an "improved" more standards compliant version. 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);

The above code will break for IE8 or less :(. Making JavaScript work cross browser is a right pain.

So I you could read the documentation for browsers at

But those are not easy to read or navigate. A great visual guide for cross browser scripting is the visibone BrowserBook.

It will show cross browser support (red is firefox, blue is IE) :

enter image description here

Give it a few months and you'll know how to use all that comfortably.

生来就爱笑 2024-11-15 16:39:05

我认为您的最终 document.write 中有一些拼写错误。您在 HeroName 之后缺少一个“+”...并且您有一些重复的 document.writes。

我认为这应该是您的最终代码:

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?','');

if (heroGender == ('F')) { heroGender ='girl'; } else { heroGender = 'boy'; }
document.write('Once upon a time there was a ' + heroGender + ' named ' + heroName + '. ' + heroName + ' had a ' + petType + ' called ' + petName + '.');

I think you had some typos in your final document.write.You're missing a '+' after heroName... and you've got some duplicative document.writes.

Here's what I believe should be your final code:

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?','');

if (heroGender == ('F')) { heroGender ='girl'; } else { heroGender = 'boy'; }
document.write('Once upon a time there was a ' + heroGender + ' named ' + heroName + '. ' + heroName + ' had a ' + petType + ' called ' + petName + '.');
○闲身 2024-11-15 16:39:05

此行中有一个语法错误:

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

具体而言,此处:

heroName '.''<BR>' + heroName

如果将其更改为此,则应该按预期工作:

document.write(' named ' + heroName + ' who had a ' + petType + ' called ' + petName + '.');

请注意,我还删除了该短语第一部分中的冗余,因为您已经将其包含在“if”之前“F”,然后是“女孩””声明。

There is a syntax error in this line:

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

specifically, here:

heroName '.''<BR>' + heroName

If you change it to this, your should work as expected:

document.write(' named ' + heroName + ' who had a ' + petType + ' called ' + petName + '.');

Notice that I also removed the redundancy from the first part of the phrase, since you already included it before your "if 'F', then 'girl'" statement.

So尛奶瓶 2024-11-15 16:39:05

太多引号和缺少的加号

Error: missing ) after argument list
Source File: http://fiddle.jshell.net/_display/
Line: 41, Column: 83
Source Code:
document.write('Once upon a time there was a ' + heroGender + ' named ' + heroName '.''<BR>' + 

应该是

document.write(' named ' + heroName + '.<BR>' + 
heroName + ' had a ' + petType + ' called ' + petName + '.');

http://jsfiddle.net/mplungjan/zHGsD/

Too many quotes and a missing plus

Error: missing ) after argument list
Source File: http://fiddle.jshell.net/_display/
Line: 41, Column: 83
Source Code:
document.write('Once upon a time there was a ' + heroGender + ' named ' + heroName '.''<BR>' + 

should be

document.write(' named ' + heroName + '.<BR>' + 
heroName + ' had a ' + petType + ' called ' + petName + '.');

http://jsfiddle.net/mplungjan/zHGsD/

你穿错了嫁妆 2024-11-15 16:39:05
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 + '.');

I got it to run by adding the two + that I've wrapped in **

俯瞰星空 2024-11-15 16:39:05

问题出在你的最后一行。更正如下

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

The problem is on your last line. Corrected as follow

document.write('Once upon a time there was a ' + heroGender + ' named ' + heroName + '.' + '<BR>' + heroName + ' had a ' + petType + ' called ' + petName + '.');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文