简体   繁体   中英

loop through arrays and output results to html

I've got this method speak(), which takes two arguments. It's a property of the prototype, so multiple objects will use it.

I'd like to grab those values it returns, loop through them, and output them to my html. The part I can't figure out is, how do I target each individual paragraph tag to correspond with the output of each from each of my variables generated results? Would this require a double loop? I'm lost. var para = document.querySelectorAll('p');

var speak = function(what, job) {
    var whoWhat = this.name + ' says, ' + what,
        whoJob = this.name + "'s job is: " + job;
    console.log(whoWhat);
    console.log(whoJob);
    return whoWhat, whoJob;
};

function Peep(name, job) {
    this.name = name;
    this.job = job;
}

Peep.prototype.speak = speak;

var randy = new Peep('Randy', 'lawyer');
randy.speak('"blahblah"', randy.job);

var mandy = new Peep('Mandy', 'mom');
mandy.speak('"woooooaahhhh"', mandy.job);

Here's a jsfiddle

Check this one - jsFiddle

Keep adding the HTML to a text. And finally add them to the DOM.

var speak = function(what, job) {
    var whoWhat = this.name + ' says, ' + what,
        whoJob = this.name + "'s job is: " + job;
    console.log(whoWhat);
    console.log(whoJob);
    return "<p>"+whoWhat+", "+whoJob+"</p>";
};

var txt = "";
var randy = new Peep('Randy', 'lawyer');
txt+=randy.speak('"blahblah"', randy.job);

var mandy = new Peep('Mandy', 'mom');
txt+=mandy.speak('"woooooaahhhh"', mandy.job);
document.getElementById('result').innerHTML = txt;

//in HTML add the result node
<body>
  <p id='result'>
  </p>
</body>

Using JavaScript you can access the DOM (Document Object Model) and can append new elements to existing elements. For example, you could create a new paragraph element and add this paragraph element to an existing div with the id "result". Here is an example:

 var appendText = function (text, parentId) {
     var para = document.createElement("p");
     var node = document.createTextNode(text);
     para.appendChild(node);
     var parentElement = document.getElementById(parentId);
     parentElement.appendChild(para);
 }

 var speak = function (what, job) {
     var whoWhat = this.name + ' says, ' + what,
         whoJob = this.name + "'s job is: " + job;
     return [whoWhat, whoJob];
 };

 function Peep(name, job) {
     this.name = name;
     this.job = job;
 }

 Peep.prototype.speak = speak;

 var randy = new Peep('Randy', 'lawyer');
 var randySays = randy.speak('"blahblah"', randy.job);
 appendText(randySays[0], "result");
 appendText(randySays[1], "result");

 var mandy = new Peep('Mandy', 'mom');
 var mandySays = mandy.speak('"woooooaahhhh"', mandy.job);
 appendText(mandySays[0], "result");
 appendText(mandySays[1], "result");

Here is the jsfiddle with the required html: http://jsfiddle.net/stH7b/2/ . You can also find more information on how to append a paragraph to the DOM here: http://www.w3schools.com/js/js_htmldom_nodes.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM