简体   繁体   English

innerHTML和jqueries .html()...... GOTCHA! 任何适当的工作?

[英]innerHTML and jqueries .html() … GOTCHA! any proper work arounds?

innerHTML and jqueries .html() ... GOTCHA! innerHTML和jqueries .html()...... GOTCHA!

I had just created a little template html tag for my javascript to go get a copy of and push into a table... It looked like this: 我刚刚为我的javascript创建了一个小模板html标签,以获取副本并推入表格......它看起来像这样:

<div id="TimesheetEntryRow">
            <tr id="row{RowID}">
               <td></td>
            </tr>
</div >

So in jquery I thought, cool let's grab that: 所以在jquery我想,很酷让我们抓住:

timesheetTemplateHtml = $( "#TimesheetEntryRow" ).html();

and then I appended it to my awesome table: 然后我把它附加到我真棒的桌子上:

$( "#TimesheetTable" ).append( timesheetTemplateHtml );

It didn't work? 它不起作用?

It had stripped off the <tr> and the <td> s and left true tags like the <input /> s I had inside it. 它已经剥离了<tr><td>并留下了真正的标签,就像我里面的<input />一样。

So I thought, oh jquery you naughty little critter, I'll use innerHTML so that I do not have to worry about jquery being clever: 所以我想,哦,你顽皮的小动物,我会使用innerHTML,所以我不必担心jquery聪明:

timesheetTemplateHtml = document.getElementById( "TimesheetEntryRow" ).innerHTML;
document.getElementById( "TimesheetTable" ).innerHTML += timesheetTemplateHtml;

Still not working? 还是行不通? Still it stripped off all the tags for my template... 它仍然剥离了我模板的所有标签......

I binged the hell out of it and stumbled across these two articles: 我把它搞砸了,偶然发现了这两篇文章:

FIREFOX: https://developer.mozilla.org/en-US/docs/Web/API/element.innerHTML?redirectlocale=en-US&redirectslug=DOM%2Felement.innerHTML FIREFOX: https//developer.mozilla.org/en-US/docs/Web/API/element.innerHTML? redirectlocale = en-US & redirectslug = DOM%2Felement.innerHTML

IE: http://msdn.microsoft.com/en-us/library/ms533897(VS.85).aspx IE: http//msdn.microsoft.com/en-us/library/ms533897(VS.85).aspx

GREAT, so that function I've used my entire life has been a LIE I tell you. 太棒了,所以我一生都用过的功能一直是我告诉你的谎言。 In fact, the only browser it does work in is Chrome ( I think they are ignoring the standards or something? ). 事实上,它唯一可用的浏览器是Chrome(我认为它们忽略了标准或什么?)。

I managed to figure out that if you put the entire tag around the it, seems to copy it okay... Which is very annoying but has saved my bacon on this occasion. 我设法弄清楚,如果你把整个标签放在它周围,似乎可以复制它好......这很烦人但是在这个场合保存了我的培根。

<div id="TimesheetEntryRow">
            <table>
                <tr id="row{RowID}">
                   <td></td>
                </tr>
            </table>
</div>

Now the above makes sense, but I really did not know innerHTML also does html parsing to check you're not being silly... Can any of you think of another way of not having to put an entire tag set in my templater? 现在上面的内容是有道理的,但是我真的不知道innerHTML也会做html解析来检查你是不是很傻......你们有没有想到另一种不必在我的模板中放置整个标签的方法?

Crazy crazy crazy! 疯狂疯狂疯了!

Related posts: 相关文章:


My full example template is this: 我的完整示例模板是这样的:

<div id="TimesheetEntryRow" style="display:none;">
    <table>
        <tbody>
            <tr id="row{RowID}">
                <td>blah</td>
                <td class="timeSpent"><input name="timeMon[]" type="text" class="form-control inputTimeSpent timeSpent timeMon" placeholder="0" /></td>
                <td class="timeSpent"><input name="timeTue[]" type="text" class="form-control inputTimeSpent timeSpent timeTue" placeholder="0" /></td>
                <td class="timeSpent"><input name="timeWed[]" type="text" class="form-control inputTimeSpent timeSpent timeWed" placeholder="0" /></td>
                <td class="timeSpent"><input name="timeThur[]" type="text" class="form-control inputTimeSpent timeSpent timeThur" placeholder="0" /></td>
                <td class="timeSpent"><input name="timeFri[]" type="text" class="form-control inputTimeSpent timeSpent timeFri" placeholder="0" /></td>
                <td class="timeSpent timeSpentWeekend"><input name="timeSat[]" type="text" class="form-control inputTimeSpent timeSpent timeSat" placeholder="0" /></td>
                <td class="timeSpent timeSpentWeekend"><input name="timeSun[]" type="text" class="form-control inputTimeSpent timeSpent timeSun" placeholder="0" /></td>
                <td class="timeSpent"><input disabled type="text" class="form-control timeSpent allocated" /></td>
                <td class="timeSpent"><input disabled type="text" class="form-control timeSpent total" value="0" /></td>
                <td><textarea name="note[]" class="form-control inputNotes" placeholder="Enter notes"></textarea></td>
            </tr>
        </tbody>
    </table>
</div>

NOTE TO EVERYONE : If you use clone, do not forget to display:block on the css style. 给每个人的注意事项 :如果您使用克隆,请不要忘记在CSS样式上显示:block。

Anyways, even with clone, the above example will not do what you expect (if you think it does, actually go inspect the element and it would have stripped the whole load out) 无论如何,即使使用克隆,上面的示例也不会按照您的预期执行(如果您认为这样做,实际上会检查元素并且它会剥离整个负载)

The problem that you're experiencing is due to the fact that the template isn't valid HTML. 您遇到的问题是由于模板不是有效的HTML。 <tr> elements must be inside of a <table> , <tbody> , <thead> , <tfoot> , and maybe something else I'm forgetting. <tr>元素必须位于<table><tbody><thead><tfoot> ,也许还有其他我忘记的东西。 When the browser sees the <tr> elements in an invalid place, it just deletes them and moves on, leaving the content where it is. 当浏览器在无效位置看到<tr>元素时,它只是删除它们并继续前进,将内容保留在原来的位置。 (Same for the <td> s not in a <tr> s.) Therefore, what you can do is to make your template element a <table> : (对于不在<tr><td>相同。)因此,您可以做的是使您的模板元素成为<table>

<table id="TimesheetEntryRow" style="display:none">
    <tr>
        <td>blah</td>
    </tr>
</table>

See a working example here: http://jsfiddle.net/6GSJ4/2/ 在这里查看一个工作示例: http//jsfiddle.net/6GSJ4/2/

I know that some frameworks such as AngularJS (one I work with) allow you to put your templates inside of <script type="text/template"> elements since those are not processed as DOM elements by the HTML parser so all of your elements will stay intact. 我知道一些框架,例如AngularJS(我使用的一个)允许您将模板放在<script type="text/template">元素中,因为HTML解析器不会将这些<script type="text/template">作为DOM元素处理,因此您的所有元素都是如此将保持完整。 That may be an option if you need it (though .innerHTML obviously wouldn't work). 如果你需要它可能是一个选项(虽然.innerHTML显然不起作用)。

You could of course just add the html back to the template tag right away. 您当然可以立即将html添加回模板标签。 Not optimal, but will get the job done 不是最佳的,但会完成工作

timesheetTemplateHtml = $("#TimesheetEntryRow").html();

$("#TimesheetEntryRow").html(timesheetTemplateHtml);

I would do it like this. 我会这样做的。 Simply use a table for your template without displaying it: 只需使用表格为您的模板而不显示它:

<table id="TimesheetEntryRow" style="display: none;">
  <tr>
    <td>aaa</td>
  </tr>
</table>

and than just select the template and the destination once on document ready. 而不仅仅是在准备文件时选择模板和目的地。 When needed, clone the template and append it to the destination: 需要时,克隆模板并将其附加到目标:

$(function(){

  var $table = $( "#TimesheetTable" );
  var $row = $( "#TimesheetEntryRow tr" );

  $('#button').on('click', function(){
    $table.append($row.clone());
  });

});

See a working example here: http://codepen.io/timbuethe/pen/czJla/ 在这里查看一个工作示例: http//codepen.io/timbuethe/pen/czJla/

You can "misuse" the script tag like this: 您可以像这样“滥用”脚本标记:

<script type="text/template" id="TimesheetEntryRow">
    <tr id="row{RowID}">
        <td></td>
    </tr>
</script>

Some JavaScript templating scripts use this, eg Underscore.js 一些JavaScript模板脚本使用它,例如Underscore.js

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

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