简体   繁体   English

Javascript DOM操作:更改元素的文本

[英]Javascript DOM manipulation: changing text of an element

I tried to print the results of the function calculation() to the text area but it seems not working. 我试图将函数calculation()的结果打印到文本区域,但似乎不起作用。 Can you please help! 你能帮忙吗?

<head>
  <title>Lab 6</title>
<script>
function calculate(){
for (var i=100; i<1000; i++) {
var x = i%10;
var y = Math.floor((i/10)%10);
var z = Math.floor((i/100)%10);
 if (i== x*x*x +y*y*y + z*z*z) {

document.getElementbyTagname("textarea").innerHtml = i;
document.getElementbyTagname("button").addEventListener("click",
calculate());
}}} 

</script>
</head>

<body>
<textarea rows="4" cols="50"> </textarea>
<button type="button">Click Me!</button>
</body>

</html>

Hi I have edit your code to make it work, I don't really get your logic, so not sure if that is what you want. 嗨,我已经编辑了您的代码以使其正常工作,我并没有真正理解您的逻辑,所以不确定是否正是您想要的。 But you can look at the my syntax to refine your own code. 但是您可以查看my语法来完善自己的代码。 Also You made several mistakes. 您也犯了几个错误。 1. 'getElementbyTagname' should be 'getElementsByTagName' 2. you didn't put the event function to the right place to trigger the click event. 1.'getElementbyTagname'应该是'getElementsByTagName'2.您没有将事件函数放到正确的位置来触发click事件。

`var btn = document.querySelector(' .btn') btn.addEventListener('click', function(e){ calculate() `var btn = document.querySelector('.btn')btn.addEventListener('click',function(e){calculate()

})
function calculate(){
      for (var i=100; i<1000; i++) {
                var x = i%10;
                var y = Math.floor((i/10)%10);
                var z = Math.floor((i/100)%10);
                if (i== x*x*x +y*y*y + z*z*z) {
                    var text = document.querySelector(' .demo')
                   text.innerHTML = i

                }}}`

I try to rewrite your code. 我尝试重写您的代码。 try once may this help... 尝试一次,可能有帮助...

<head>
  <title>Lab 6</title>
<script>
function calculate()
{
    for (var i=100; i<1000; i++) 
    {
        var x = i%10;
        var y = Math.floor((i/10)%10);
        var z = Math.floor((i/100)%10);
        if (i== x*x*x +y*y*y + z*z*z) 
        {

            document.getElementsByTagName("textarea")[0].innerHtml = i;

        }
    }
} 
document.getElementsByTagName("button")[0].addEventListener("click",calculate());
</script>
</head>

<body>
<textarea rows="4" cols="50"> </textarea>
<button type="button">Click Me!</button>
</body>

Try this out. 试试看 Made many changes to your code, 对您的代码进行了许多更改,

 function calculate(){ var txt=""; for (var i=100; i<1000; i++) { var x = i%10; var y = Math.floor((i/10)%10); var z = Math.floor((i/100)%10); if (i== x*x*x +y*y*y + z*z*z) { txt = txt + i + "\\n"; } } document.getElementById("textarea").innerHTML = txt; } 
 <html> <head> <title>Lab 6</title> </head> <body> <textarea rows="4" cols="50" id="textarea"> </textarea> <button type="button" onclick="calculate();">Click Me!</button> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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