admin管理员组

文章数量:1332389

I want to have my javascript print a random quote out of an array. I managed to do this, but don't know how to control where it writes. I wish to write it within a div. Help?

Code:

quotes = [];
quotes[0] = "I have a new philosophy. I'm only going to dread one day at a time.";
quotes[1] = "Reality is the leading cause of stress for those in touch with it.";
quotes[2] = "Few things are harder to put up with than the annoyance of a good example.";
quotes[3] = "The pure and simple truth is rarely pure and never simple.";
quotes[4] = "There's no business like show business, but there are several businesses like accounting.";
quotes[5] = "Man invented language to satisfy his deep need to plain.";

//calculate a random index
index = Math.floor(Math.random() * quotes.length);

//display the quotation
document.write("<p>" +  quotes[index] + "</p>");

I want to have my javascript print a random quote out of an array. I managed to do this, but don't know how to control where it writes. I wish to write it within a div. Help?

Code:

quotes = [];
quotes[0] = "I have a new philosophy. I'm only going to dread one day at a time.";
quotes[1] = "Reality is the leading cause of stress for those in touch with it.";
quotes[2] = "Few things are harder to put up with than the annoyance of a good example.";
quotes[3] = "The pure and simple truth is rarely pure and never simple.";
quotes[4] = "There's no business like show business, but there are several businesses like accounting.";
quotes[5] = "Man invented language to satisfy his deep need to plain.";

//calculate a random index
index = Math.floor(Math.random() * quotes.length);

//display the quotation
document.write("<p>" +  quotes[index] + "</p>");
Share Improve this question edited Jan 16, 2013 at 23:04 Josue Espinosa asked Jan 16, 2013 at 23:01 Josue EspinosaJosue Espinosa 6231 gold badge5 silver badges12 bronze badges 3
  • 2 Use DOM manipulation. w3/wiki/Creating_and_modifying_HTML . document.write creates the output where it was called or replaces the document when it was already pletely loaded. It has to be used very carefully and is not very useful in most cases. – Felix Kling Commented Jan 16, 2013 at 23:03
  • Just create a div in your HTML (assuming you have an existing HTML document) and then use getElementById() to access the div, and add your content to it. – matthewpavkov Commented Jan 16, 2013 at 23:04
  • 1 I've got nothing useful to say, but those quotes are pretty amusing! – Paul Commented Apr 24, 2013 at 19:05
Add a ment  | 

4 Answers 4

Reset to default 8

Give the div an id say divid then use document.getElementById() to get the div then innerHTML to set its content;

quotes = [];
quotes[0] = "I have a new philosophy. I'm only going to dread one day at a time.";
quotes[1] = "Reality is the leading cause of stress for those in touch with it.";
quotes[2] = "Few things are harder to put up with than the annoyance of a good example.";
quotes[3] = "The pure and simple truth is rarely pure and never simple.";
quotes[4] = "There's no business like show business, but there are several businesses like accounting.";
quotes[5] = "Man invented language to satisfy his deep need to plain.";

//calculate a random index
index = Math.floor(Math.random() * quotes.length);

//display the quotation
document.getElementById('divid').innerHTML = "<p>" +  quotes[index] + "</p>";

You should use a selector to get your div and write it's innerHtml property:

document.getElementById("yourDivsId").innerHTML=quotes[index];

You can use:

document.getElementById("divID").innerHTML=quotes[index];

or you can use JQuery

$("#divID").html(quotes[index]);

document.write will write exactly where it stands. There is no conditional target for document.write to end up in. However, what you are looking for is some DOM manipulation.

You should target your div, and then place the text in there like this:

html

<div id="divId"></div>

js

quotes = [];
quotes[0] = "I have a new philosophy. I'm only going to dread one day at a time.";
quotes[1] = "Reality is the leading cause of stress for those in touch with it.";
quotes[2] = "Few things are harder to put up with than the annoyance of a good example.";
quotes[3] = "The pure and simple truth is rarely pure and never simple.";
quotes[4] = "There's no business like show business, but there are several businesses like accounting.";
quotes[5] = "Man invented language to satisfy his deep need to plain.";

//calculate a random index
index = Math.floor(Math.random() * quotes.length);

var mydiv = document.getElementById("divId");
mydiv.innerHTML = '<p>' +  quotes[index] + '</p>';

本文标签: javascripthow to control where documentwrite writesStack Overflow