如何使用javascript创建通用html
how to create generic html with javascript
我有以下html:
<div id="prog" class="downloads clearfix">
<div class="item">
<div class="image_container">
<img src="/img/downloads/company.png" width="168" height="238" alt="">
</div>
<div class="title">
pricelist: <label id="pr1"></label>
</div>
<div class="type">
pdf document
</div>
<div class="link">
<a id="pdfdocument" class="button" target="_blank" href="#">start Download </a>
</div>
</div>
</div>
我想用Javascript构建<div id="prog">
内部的HTML:
<div id="prog" class="downloads clearfix"></div>
我正在尝试使用这个Javascript,但没有成功:
var tmpDocument, tmpAnchorTagPdf, tmpAnchorTagXls, parentContainer, i;
parentContainer = document.getElementById('prog');
for (i = 0; i < documents.length; i++) {
tmpDocument = documents[i];
tmpAnchorTagPdf = document.createElement('a id="pdfdocument" ');
tmpAnchorTagPdf.href = '/role?element=' + contentElement.id + '&handle=' + ope.handle;
tmpAnchorTagPdf.innerHTML = 'start Download';
tmpAnchorTagXls = document.createElement('a');
tmpAnchorTagXls.href = '/role?element=' + contentElement.id + '&handle=' + ope.handle;
tmpAnchorTagXls.innerHTML = 'start Download';
parentContainer.appendChild(tmpAnchorTagPdf);
parentContainer.appendChild(tmpAnchorTagXls);
}
如果这是一段将要多次使用的代码,可以采用以下方法。
这是原始的div
,没有您想要创建的代码:
<div id="prog" class="downloads clearfix">
</div>
在类似于的隐藏div
中创建模板
<div id="itemtemplate" style="display: none;">
<div class="item">
<div class="image_container">
<img src="/img/downloads/company.png" width="168" height="238" alt="">
</div>
<div class="title">
pricelist: <label></label>
</div>
<div class="type">
pdf document
</div>
<div class="link">
<a class="button" target="_blank" href="#">start Download </a>
</div>
</div>
</div>
然后用jquery复制它(OP最初有一个jquery标记;JS见下文),更新复制的div中的一些HTML,然后将其添加到文档中
function addItem() {
var item = $("#itemtemplate div.item").clone();
//then you can search inside the item
//let's set the id of the "a" back to what it was in your example
item.find("div.link a").attr("id", "pdfdocument");
//...the id of the label
item.find("div.title label").attr("id", "pr1");
//then add the objects to the #prog div
$("#prog").append(item);
}
更新
对于这个使用纯Javascript的示例,这里有相同的addItem()
函数:
function JSaddItem() {
//get the template
var template = document.getElementById("itemtemplate");
//get the starting item
var tempitem = template.firstChild;
while(tempitem != null && tempitem.nodeName != "DIV") {
tempitem = tempitem.nextSibling;
}
if (tempitem == null) return;
//clone the item
var item = tempitem.cloneNode(true);
//update the id of the link
var a = item.querySelector(".link > a");
a.id = "pdfdocument";
//update the id of the label
var l = item.querySelector(".title > label");
l.id = "pr1";
//get the prog div
var prog = document.getElementById("prog");
//append the new div
prog.appendChild(item);
}
我把两种方法都放在了一起。
相关文章:
- 从html创建一个指令,该指令按类名应用函数
- 用HTML创建一个下拉框的简单方法,只需包含0到x
- 基于当前内部HTML创建动态jQuery变量名称
- mootools:从 HTML 创建一个新的 DOM 元素
- 使用 flash、javascript、classic asp 从 html 创建 CSV
- JQuery + 从数组中为 html 创建单选按钮
- 100%的高度在主体和html创建滚动问题
- 使用JavaScript与常规HTML创建HTML元素
- 用HTML创建JavaScript数组
- 从HTML创建base64字符串
- 如何仅使用JS和HTML创建按钮
- 如何通过jquery .html创建一个href,然后使用它的事件
- 在用javascript和html创建程序时遇到问题
- 如何使用引导程序和HTML创建联系人表单
- 如何使用Javascript和HTML创建一个文档文件
- 用HTML创建登录页面
- 使用Canvas和Html 5创建类似flash的动画
- 使用jQuery从HTML创建一个JSON对象
- 如何禁用由html创建的特定dijit contentPane选项卡
- 用PHP, JavaScript和Html创建一个简单的购物车