简体   繁体   English

如何从模板而不是html获取dom元素

[英]how to get a dom element from a template not html

here's the jsfiddle at first try the script as is and then comment the last line and uncomment the one before it that will give me the functionality i want but with the wrong data, basically i want to grub a template and insert data into it and the insert it before another element in the page. 这是jsfiddle ,首先尝试按原样尝试脚本,然后注释最后一行,然后取消注释之前的那一行,这将为我提供我想要的功能,但数据错误,基本上我想获取模板并将数据插入其中,然后将其插入页面中的另一个元素之前。 the problem is that the method that i'm using requires me to use an object or elementNode to insert it but after fetching the template i'm left with html wich result in an error : 问题是我使用的方法要求我使用一个object或elementNode来插入它,但是在获取模板后,我留下的html夹将导致错误:

Error: NOT_FOUND_ERR: DOM Exception 8 错误:NOT_FOUND_ERR:DOM异常8

the html: HTML:

<section class="container well">
    <header></header>    
    <section id="section">
        <h4 id="heading">heading 4</h4>
        <div id="div_1" class="divs">
            <a href="#">file_1.jpg</a>
            <button class="btn btn-mini btn-danger pull-right">&#10006;</button>
        </div>
        <div id="div_2" class="divs">
            <a href="#">file_2.jpg</a>
            <button class="btn btn-mini btn-danger pull-right">&#10006;</button>
        </div>
        <div id="div_3" class="divs">
            <a href="#">file_3.jpg</a>
            <button class="btn btn-mini btn-danger pull-right">&#10006;</button>
        </div>
        <div id="div_4" class="divs">
            <a href="#">file_4.jpg</a>
            <button class="btn btn-mini btn-danger pull-right">&#10006;</button>
        </div>
        <div class="clearfix">
            <button class="btn btn-primary pull-right add">
                 Add items
            </button>
        </div>
    </section>
    <footer></footer>
</section>
<script type="template" id="template">
    <div id="{{id}}">
        <a href="#">{{fileName}}</a>
    </div>
</script>​

my javascript code: 我的JavaScript代码:

$(document).on('click', '.add', function(e){
    e.preventDefault();
    e.stopPropagation();
    target   = document.getElementById($('.divs')[0].id);
    template = document.querySelector('#template').innerHTML;
    div      = template
                       .replace(/{{id}}/g, '0')
                       .replace(/{{fileName}}/g, 'file_0');
    //target.parentNode.insertBefore(document.createTextNode('AZERTY'), target);
    target.parentNode.insertBefore(div, target);
});

the jsfiddle again and thanks in advance. jsfiddle再次感谢。

You cannot insert div , which is a string containing HTML code, into the DOM as if it were an element. 您不能将div (包含HTML代码的字符串)插入DOM中,就好像它是一个元素一样。 Instead, you could create another div and set its innerHTML . 相反,您可以创建另一个div并设置其innerHTML

$(document).on('click', '.add', function(e){
    e.preventDefault();
    e.stopPropagation();
    target   = document.getElementById($('.divs')[0].id);
    template = document.querySelector('#template').innerHTML;
    div      = template
                        .replace(/{{id}}/g, '0')
                        .replace(/{{fileName}}/g, 'file_0');
    outerDiv = document.createElement('div');
    outerDiv.innerHTML = div;
    target.parentNode.insertBefore(outerDiv, target);
});

As noted by a comment on another answer, you may want to skip having a containing div within the template, as you will end up with two divs unnecessarily. 正如对另一个答案的评论所指出的那样,您可能希望跳过在模板中包含一个包含div的内容,因为最终将不必要地得到两个div。 Instead you could have simply 相反,您可以简单地

    <a href="#">{{fileName}}</a>

as the template and then 作为模板,然后

div = document.createElement('div');
div.id = '0';
div.innerHTML = template.replace(/{{fileName}}/g, 'file_0');
target.parentNode.insertBefore(div, target);

in the script. 在脚本中。 Another alternative is simply to insert the HTML string directly into the target: 另一种选择是简单地将HTML字符串直接插入目标中:

    target.innerHTML = div + target.innerHTML;

Here is an article - with several links and lots of good advice - about manipulating templates with jQuery: 这是一篇有关使用jQuery处理模板的文章-包含多个链接和很多好的建议-

 $('.add').on('click', function(e){
      e.preventDefault();
      e.stopPropagation();
      target   = document.getElementById($('.divs')[0].id);
      template = document.querySelector('#template').html();
      div      = template.replace(/{{id}}/g, '0').replace(/{{fileName}}/g, 'file_0');
      divtag = document.createElement('div');
      divtag.innerHTML = div;
      divtag.id = template.id;
      target.parentNode.insertBefore(divtag, target);
 }

This should work 这应该工作

here's the answer : 答案是:

code: 码:

$(document).on('click', '.add', function(e){
    e.preventDefault();
    e.stopPropagation();
    target   = document.getElementById($('.divs')[0].id);
    template = document.querySelector('#template').innerHTML;
    div      = template
                        .replace(/{{id}}/g, '0')
                        .replace(/{{fileName}}/g, 'file_0.jpg');
    new_div  = document.createElement('div');
    new_div.setAttribute('class', 'divs');
    new_div.setAttribute('id', 'div_' + i);
    new_div.innerHTML = div;
    target.parentNode.insertBefore(new_div, target);
});

​ thanks to stuart. 感谢斯图尔特。

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

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