脚本会产生我找不到的错误

时间:2011-05-17 20:09:35

标签: javascript document.write

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

  

曾几何时,有一个名为NAME的性别人员,他有一个名为PET NAME的PET。

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

6 个答案:

答案 0 :(得分:7)

在等待书籍的时候!

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

给它几个月,你会知道如何舒适地使用它。

答案 1 :(得分:2)

我认为你在最终的document.write中有一些拼写错误。你在heroName之后错过了一个'+'...而且你有一些重复的文档。写的。

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

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 + '.');

答案 2 :(得分:1)

报价太多,缺少加号

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/

答案 3 :(得分:1)

此行中存在语法错误:

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',然后是'girl'”语句之前将其包括在内。

答案 4 :(得分:1)

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

我通过添加+

中包含的两个**来运行它

答案 5 :(得分:0)

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

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