简体   繁体   English

动态向网站添加内容

[英]Dynamically adding content to the website

I have a website, and on the main page, there is a form to create div's on that same page. 我有一个网站,并且在主页上有一种可在同一页面上创建div的表格。 The form has entries of name of the div, color of the div, coordinates of the upper left corner of the div (for example, the entry can be [50 70]) and the coordinates of the lower right corner of the div (for example, the entry can be [780 800]). 表格中包含div名称,div颜色,div左上角的坐标(例如,该条目可以为[50 70])和div右下角的坐标(对于例如,条目可以是[780 800]。 I need the code that will enable me to dynamically create div's on the page with the specified parameters, so I don't have to edit the source code myself - so that the user can come in, fill out a form, and the div to be created. 我需要使我能够使用指定参数在页面上动态创建div的代码,因此我不必自己编辑源代码-这样用户就可以输入,填写表格,然后将div被创建。 Does anyone have any ideas on how this can be done? 有谁对如何做到这一点有任何想法吗? Thanks in advance. 提前致谢。

You can work around by understanding this code. 您可以通过理解此代码来解决。

HTML 的HTML

<input type="text" name="value" id='value' />
<input type="button" value="submit" onclick="createDiv()" />
</p>

JS: JS:

function createDiv() {
        var divName = document.getElementById('value').value; //getting value for div name
        var iDiv = document.createElement('div'); //creating div element
        iDiv.name = divName;
        document.getElementsByTagName('body')[0].appendChild(iDiv) //adding it in DOM tree
    }

Here you go 干得好

 function CreateDiv() { var name = document.getElementById('name').value; var color = document.getElementById('color').value; var ulcorner = document.getElementById('ulcorner').value; var brcorner = document.getElementById('brcorner').value; var top = parseInt(ulcorner.split(',')[0]); var left = parseInt(ulcorner.split(',')[1]); var bottom = parseInt(brcorner.split(',')[0]); var right = parseInt(brcorner.split(',')[1]); var newDiv = document.createElement("div"); newDiv.style.backgroundColor = color; newDiv.style.top = top + 'px'; newDiv.style.left = left + 'px'; newDiv.style.height = (top + bottom) + 'px'; newDiv.style.width = (left + right) + 'px'; var container = document.getElementById("container"); container.appendChild(newDiv); } 
 Name : <input type='text' id='name' /><br /> Color : <input type='text' id='color' /><br /> Coordinates of the upper left corner : <input type='text' id='ulcorner' placeholder='xx,xx' /><br /> Coordinates of the lower right corner : <input type='text' id='brcorner' placeholder='xx,xx' /><br /> <button onclick='CreateDiv()'>Create Div</button> <div id='container'></div> 

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

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