简体   繁体   English

用Javascript创建动态HTML

[英]Creating Dynamic HTML with Javascript

I have been trying to dynamically append HTML input forms using a JS function to a webpage but am having trouble inserting them into the correct location. 我一直在尝试使用JS函数将HTML输入表单动态附加到网页上,但是将它们插入正确的位置时遇到了麻烦。 I have tried to create an empty div tag on line 40: 我试图在第40行上创建一个空的div标签:

<div class="itemInfo"></div>

However, when I try to locate that tag from within my JS function, with the findElementById() function it seems not to find it. 但是,当我尝试使用findElementById()函数从我的JS函数中查找该标签时,似乎找不到它。 I want to create a copy of the three input forms above the empty tag I created and append them underneath, but no matter what I have tried I have not been able to figure out how to put the forms in that exact location. 我想在我创建的空标签上方创建三个输入表单的副本,并将其附加在下面,但是无论我尝试了什么,我都无法弄清楚如何将表单放置在该确切位置。 My JS function is as follows: 我的JS功能如下:

function newItem(){
  instance++;
  var oldInput = document.getElementById("itemInfo");
  var newDiv = document.createElement("INPUT");
  newDiv.name = "myinput";
  newDiv.value = "Enter Here";
  oldInput.insertBefore(newDiv);
}

Rather than creating the forms again in this function, I would like to duplicate the following and simply append it after itself: 我不想在此函数中再次创建表单,而是要复制以下内容,并将其简单地附加在其自身之后:

<p>Item: <input type="text" name="item"/> 
   Qty: <input type="text" name="qty"/>
   Color: <input type="text" name="color"/></p>
    <input type ="button" value="Add Item" onclick="newItem();"/>
    <div class="itemInfo"></div>

I tried to wrap the three forms in a tag and calling that by Id, but it did not seem to work either, which is why I tried to make an empty tag after it. 我试图将这三种形式包装在一个标签中,并通过ID进行调用,但它似乎也不起作用,这就是为什么我尝试在其后制作一个空标签。 I have searched everywhere and there is a lot of information regarding similar issues but I can't seem to apply the solutions to my situation. 我到处搜索,并且有很多关于类似问题的信息,但是我似乎无法将解决方案应用于我的情况。 I really appreciate any help. 我非常感谢您的帮助。 Here is the entire page: 这是整个页面:

    <!DOCTYPE html>
<html>
  <head>
    <script type ="text/javascript">
      var instance = 0;

    function newItem(){
      instance++;
      var oldInput = document.getElementById("itemInfo");
      var newDiv = document.createElement("INPUT");
      newDiv.name = "myinput";
      newDiv.value = "Enter Here";
      oldInput.insertBefore(newDiv);
    }

    </script>
    <title>Welcome to New Age Embroidery!</title>
    <style type="text/css">
     body {font-family:sans-serif;color:#4f494f;}
     form input {border-radius: 7.5px;}
     h5 {display: inline;}
     .label {text-align: right}
     .ordersBook {float:left; padding-top: 10px;}
     .name {width:100%;float:left; padding:3px;}
     .wrapper { padding-left: 25px; padding-top: 20px}
    </style>
   </head>
   <body>
   <input type ="button" id="btnAdd" value="Add Item" onclick="newItem();"/>
    <div class = "wrapper">
     <h1>Welcome to New Age Embroidery!</h1>
     <div class="ordersBook_input">
     <form method ="post" class="form" action = "/newguest" method = 'post'>
       Name: <input type="text" name="name"/>

       <p>Item: <input type="text" name="item"/> 
       Qty: <input type="text" name="qty"/>
       Color: <input type="text" name="color"/></p>
        <input type ="button" value="Add Item" onclick="newItem();"/>
        <div class="itemInfo"></div>

       <p>Phone: <input type="text" name="phone"/>
       Email: <input type="text" name="email"/>
       Artwork: <input type="file" name="file"/>
       <p>Quote: <input type="text" name="quote"/></p>
     </p>
       <p>Notes: <textarea cols="40" rows="10"></textarea>
     </p>
       <input type="submit" value='Add Order'/>
     </form>
     </div>
     <div class ="ordersBook">
      <h3>Orders</h3>
      %for name in mynames:
      <div class="name">
      <h5>Name:</h5> {{name['name']}}
       <h5>Phone:</h5>{{name['phone']}}
       <h5>Email:</h5>{{name['email']}}
       <form action="/view/{{name['_id']}}" method='GET' ><input type="submit" value="View">
       </form>
       <form action="/remove/{{name['_id']}}" method='POST'> <input type="submit" value="Remove" onClick="confirm('Are you sure you want to permenantly remove this order?')">

       </form>
     </div>
     %end
     </div>
    </div>
   </body>
</html>

I changed your Javascript to this 我将您的Java语言更改为此

function newItem(){  
     newInput="<p>Item: <input type="+"text"+" name="+"item"+"/></p>";
     document.getElementById("itemInfo").innerHTML=newInput;
   }

Here's a working FIDDLE 这是一个工作场所

You are trying to use the function document.getElementById("itemInfo"); 您正在尝试使用函数document.getElementById("itemInfo"); which looks for the id itemInfo . 查找id itemInfo There is no element with the id itemInfo in your page. 您的页面中没有ID为itemInfo元素。 Create the div as follows: 如下创建div

<div class="itemInfo" id="itemInfo"></div>

This should help you get a reference of the div element. 这应该有助于您获得div元素的引用。

EDIT: The Error is in the function, node.insertBefore(new Element,reference Element); 编辑:错误在函数node.insertBefore(new Element,reference Element); is the correct function. 是正确的功能。

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

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