2016-12-08 2 views
0

Я пытаюсь создать очень простую сумасшедшую библиотеку, используя Javascript. Я в основном закончен, но мой код не будет работать. Есть ли что-то, что я забываю добавить или добавить неправильно?Generate Simple Mad Lib

HTML:

<body> 
    <h1>Mad Libs</h1> 

<div id="story"> 
    <p>A thing:</p> 
    <input type="text" id="thing"> 
    <p>A name:</p> 
    <input type="text" id="title"> 
    <p>An adjective ending in "ily":</p> 
    <input type="text" id="adjective"> 
    <p>A name:</p> 
    <input type="text" id="noun"> 
</div> 

<br/> 

    <button id="libbutton" onclick="madLibs()">Create lib</button> 
    <div id="mystory"></div> 

<script src="js/main.js"></script> 
</body> 

JS:

function madLibs() { 

    var mystory = document.getElementById("mystory"); 
    var thing = document.getElementById("thing").value; 
    var title = document.getElementById("title").value; 
    var adjective = document.getElementById("adjective").value; 
    var noun = document.getElementById("noun").value; 

    mystory.innerHTML = "Space: the final " + thing + ". " "These are the voyages of the starship " + title + ". Its five-year mission: to explore " + adjective + " new worlds, to seek out new life and new civilizations, to boldy go where no " + noun + " has gone before."; 
} 
+0

Открывает консоль - Видите ли вы какие-либо ошибки – tymeJV

+0

Да он говорит" Uncaught SyntaxError: Неожидаемая строка ". Я что-то упустил? –

+0

Какая строка набрасывается? (Должен сказать номер строки/щелкнуть по ней, чтобы перейти к исходному файлу) – tymeJV

ответ

0

Вы только что получили некоторые цитаты перепутались в mystory.innerHTML части.

function madLibs() { 

    var mystory = document.getElementById("mystory"); 
    var thing = document.getElementById("thing").value; 
    var title = document.getElementById("title").value; 
    var adjective = document.getElementById("adjective").value; 
    var noun = document.getElementById("noun").value; 

    mystory.innerHTML = "Space: the final " + thing + ". These are the voyages of the starship " + title + ". Its five-year mission: to explore " + adjective + " new worlds, to seek out new life and new civilizations, to boldy go where no " + noun + " has gone before."; 
} 

Working codepen here.

0

Был двойные кавычки (") рядом вещи:

function madLibs() { 

var story = document.getElementById('story').getElementsByTagName('input'); 
var myobj = {}; 
for(var i=0; i<story.length; i++){ 
    myobj[story[i].id] = story[i].value; 
} 
// add innerHTML 
document.getElementById('mystory').innerHTML = "Space: the final " + myobj.thing + ". These are the voyages of the starship " + myobj.title + ". Its five-year mission: to explore " + myobj.adjective + " new worlds, to seek out new life and new civilizations, to boldy go where no " + myobj.noun + " has gone before."; 

}