【问题标题】:how to write in a div in javascrtip?如何在javascript中写一个div?
【发布时间】:2020-02-10 04:41:58
【问题描述】:

我是 javascrit 的初学者。我想根据表单的名称写一个div。 当我在输入中写入时,它工作得很好。但是当我在 div 中写入时,它不起作用。有人可以告诉我如何根据表单的名称在 div 中写入吗?我有这个错误 Uncaught TypeError: Cannot read property 'innerHTML' of undefined.thank you.

             var mycpt =1;

             var coul = (200+(1+0.5)*mycpt*100);
             var div = document.createElement("div");
             div.setAttribute("class","title");
             div.setAttribute("id",`title${mycpt}`);
             div.setAttribute("style","background-color: silver;");
             div.setAttribute("style","position: absolute;");
             div.style.border ="1px solid blue";
             div.style.width="100px";
             div.style.height="200px";
             div.style.top="300px";
             div.style.left=coul+'px';

             var x = document.createElement("FORM");
             x.setAttribute("name","achille");
             x.setAttribute("action", "#");
             x.setAttribute("id", `chatformsss${mycpt}`);
             x.style.position="relative";
             x.style.top="70px";
             var input1 = document.createElement("INPUT");
             input1.setAttribute("type", "text");
             input1.setAttribute("name", `usernamee${mycpt}`);
             input1.setAttribute("id", `usernamee${mycpt}`);
             input1.setAttribute("value","salut");
             input1.style.width="98px";

             var div1 = document.createElement("div");
             div1.setAttribute("id", `messagesdzs${mycpt}`);

              var input2 = document.createElement("INPUT");
              input2.setAttribute("type", "text");
              //input2.setAttribute("size", "11");
              input2.setAttribute("id", `messagee${mycpt}`);
              input2.setAttribute("placeholder", "Message");
              input2.style.width="98px";

             var submit = document.createElement("INPUT");
             submit.setAttribute("type","submit");
             input1.setAttribute("type","text");

             submit.setAttribute("name","submit");
             submit.setAttribute("value","Envoyer");

             document.body.appendChild(div);
             x.appendChild(input1);
             x.appendChild(div1);
             x.appendChild(input2);
             x.appendChild(submit);
             div.appendChild(x);


   document.forms["achille"].elements[`messagesdzs${mycpt}`].innerHTML+="okokokok";



【问题讨论】:

标签: javascript dom


【解决方案1】:

我能够解决它真的花了我时间这里是答案 document.forms["achille"].querySelector(div#messagesdzs${mycpt}).innerHTML+="okokokok";

【讨论】:

    【解决方案2】:
    div.setAttribute("id",`title${mycpt}`);
    

    您的 var 名称是 mycp,最后没有 T

    【讨论】:

    • 我更正了变量的名称,谢谢。你能告诉我如何在 div 中这样写吗?document.forms["achille"].elements[messagesdzs${mycpt}].innerHTML="okokok";
    猜你喜欢
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2017-05-16
    相关资源
    最近更新 更多