admin管理员组

文章数量:1290103

I'm just trying to replace my P tag with a number list that counts from "1" to "5". But I'm making the numbering part a function. How do I call the function when replacing my P tag? Here is the code I wrote so far.

<script type="text/javascript">
function countNum() {
    var i=1;
    for (i=1;i<=5;i++) {
    document.write(i+"<br />");
    }
}
document.getElementById("paragraph").innerHTML=countNum();
</script>
</head>

<body>
<p id="paragraph">
Hello world.
</p>
</body>

I'm just trying to replace my P tag with a number list that counts from "1" to "5". But I'm making the numbering part a function. How do I call the function when replacing my P tag? Here is the code I wrote so far.

<script type="text/javascript">
function countNum() {
    var i=1;
    for (i=1;i<=5;i++) {
    document.write(i+"<br />");
    }
}
document.getElementById("paragraph").innerHTML=countNum();
</script>
</head>

<body>
<p id="paragraph">
Hello world.
</p>
</body>
Share Improve this question asked Mar 6, 2012 at 8:14 nokujinnokujin 111 gold badge1 silver badge3 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 4
function getHtml() {
    var html = '';
    var i=1;
    for (i=1;i<=5;i++) {
     html += i+"<br />";
    }
    return html;
}

Now use this method to assign to HTML.

document.getElementById("paragraph").innerHTML=getHtml();

I'm not really answering your question (two others already have), but another neat way to do what you want could be:

document.getElementById('paragraph').innerHTML = [1, 2, 3, 4, 5].join('<br>')

I use the join method on the [1, 2, 3, 4, 5] array.

You can make your function append to the paragraph like this.

function countNum () {
    var elem = document.getElementById("paragraph");
    var i = 1;
    var innerElem;
    for (i=1; i<=5; i++) {
        innerElem = document.createElement("span"); 
        innerElem.appendChild(document.createTextNode(i.toString()));
        innerElem.appendChild(document.createElement("br"));
        elem.appendChild(innerElem);
    }
}
countNum();

Just use .call instead of .innerHTML:

document.getElementById("paragraph").call=countNum();

you have to return your string, not to write to document directly:

function countNum() {
    var i=1;
    var string = "";
    for (i=1;i<=5;i++) {
        string += i+"<br />";
    }
    return string;
}

document.getElementById("paragraph").innerHTML=countNum();

<p id="paragraph">
Hello world.
</p>



   <script type="text/javascript">
function countNum() {
    var i=1;
    var result = "";

    for (i=1;i<=5;i++) {
     result  = result + i+ "<br />";
    }

    return result;
}
document.getElementById("paragraph").innerHTML=countNum();
</script>
<html>
<body>
 <p id="paragraph">
   Hello world.
 </p>
 <script type="text/javascript">
 function countNum() {
     var i=1;
     for (i=1;i<=5;i++) {
      document.write(i+"<br />");
     }
 }
 document.getElementById("paragraph").innerHTML=countNum();
 </script>
</body>
</html>

Try to put your script inside body below p tag.

本文标签: javascriptCalling a function into documentgetElementByIdStack Overflow