【问题标题】:how to create generic html with javascript如何使用 javascript 创建通用 html
【发布时间】:2014-06-02 08:23:36
【问题描述】:

我有以下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 构建位于 &lt;div id="prog"&gt; 内的 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);
}

【问题讨论】:

  • 你能描述一下发生了什么吗?你有错误吗?如果是这样,错误是什么?您是否得到与您想要的不匹配的输出?如果是这样,不想要的输出是什么?它与所需的输出有何不同?
  • 为什么不直接把 html 作为字符串做 $('#prog').html(varHTML);
  • 我不知道该怎么做
  • 这可能会有所帮助:stackoverflow.com/questions/9760328/…
  • 确定 jQuery 标签是正确的吗?您的代码在任何地方都没有提到 jQuery。

标签: javascript html dom


【解决方案1】:

如果这是您将多次使用的一段代码,您可以采用以下方法。

这里是原始的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);
}

我整理了一个JSFiddle with both approaches here

【讨论】:

  • 除了标签,问题可能与 jQuery 无关,看看 OP 如何无处使用它(实际上是相反的)。
  • 我喜欢它(template 对象)。我没有意识到这一点。不过,我在想,在这种情况下,他拥有的图像可能已经加载并且在将 div 添加到 DOM 时不必加载可能会很好。在阅读有关template 的图片时,请不要预加载。
  • @Ingo Bürk:我在回答后注意到了您上面的评论。我目前也在研究非 Jquery 解决方案。
猜你喜欢
  • 2021-01-07
  • 2011-09-30
  • 2014-11-02
  • 2019-03-29
  • 1970-01-01
  • 2020-02-27
  • 2011-04-19
相关资源
最近更新 更多