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
7 Answers
Reset to default 4function 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
版权声明:本文标题:javascript - Calling a function into document.getElementById - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741490905a2381614.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论