admin管理员组文章数量:1425365
I've included the HTML code to give the whole picture of what I am trying to understand.
<html>
<head>
<title>Echo App</title>
</head>
<body>
<p>Echo</p>
<p>Say what? <input id="sayThis" size="40" /></p>
<p>How many times?
<select id="howMany">
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
</select></p>
<p><button onClick="doLoop()">Do it!</button></p>
<p><div id="results"></div></p>
<script type="text/javascript" language="javascript">
function doLoop() {
var sayWhat = document.getElementById("sayThis").value;
var maxLoop = document.getElementById("howMany").value;
var str = ""; // where we'll store our output temporarily
for (var i=1; (i<=maxLoop); i++) {
str = str + i + ":" + " " + sayWhat + '<br>';
}
document.getElementById("results").innerHTML=str;
}
</script>
</body>
</html>
How would the code be written out for this use of 'str'
str = str + i + ":" + " " + sayWhat + '<br>';
And, more especially, how would the code be written out for this use of 'str'
document.getElementById("results").innerHTML=str;
I look forward to your replies/answers. Thank you.
I've included the HTML code to give the whole picture of what I am trying to understand.
<html>
<head>
<title>Echo App</title>
</head>
<body>
<p>Echo</p>
<p>Say what? <input id="sayThis" size="40" /></p>
<p>How many times?
<select id="howMany">
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
</select></p>
<p><button onClick="doLoop()">Do it!</button></p>
<p><div id="results"></div></p>
<script type="text/javascript" language="javascript">
function doLoop() {
var sayWhat = document.getElementById("sayThis").value;
var maxLoop = document.getElementById("howMany").value;
var str = ""; // where we'll store our output temporarily
for (var i=1; (i<=maxLoop); i++) {
str = str + i + ":" + " " + sayWhat + '<br>';
}
document.getElementById("results").innerHTML=str;
}
</script>
</body>
</html>
How would the code be written out for this use of 'str'
str = str + i + ":" + " " + sayWhat + '<br>';
And, more especially, how would the code be written out for this use of 'str'
document.getElementById("results").innerHTML=str;
I look forward to your replies/answers. Thank you.
Share Improve this question asked Mar 24, 2011 at 16:52 AudreyAudrey 11 gold badge1 silver badge2 bronze badges 1- I don't understand what you are asking. – rlb.usa Commented Mar 24, 2011 at 16:57
2 Answers
Reset to default 3your code works fine, look at it here :-)
http://jsfiddle/maniator/s8fyQ/
The reason why it works:
This is because str
is first set as an empty string, ad as you go through the loop you add on more text.
document.getElementById("results").innerHTML=str;
sets the html of the element with the id=results
to whatever is inside of the str
variable that you set earlier
UPDATE
Instead of using str
you can do:
for (var i=1; (i<=maxLoop); i++) {
document.getElementById("results").innerHTML =
document.getElementById("results").innerHTML + i
+ ":" + " " + sayWhat + '<br>';
}
If you're trying to understand why it works, it's simple.
Your code creates a new variable, adds new text to that variable in each iteration of the loop and then sets the value of innerHTML. When you set the value of innerHTML, the browser triggers a behavior which causes it to dynamically update the tag you told it (results).
本文标签: javascriptdefining a variable to output innerHTMLStack Overflow
版权声明:本文标题:javascript - defining a variable to output innerHTML - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744707896a2620949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论