Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Underline.js模板IE9/IE10未返回正确的代码_Javascript_Internet Explorer_Underscore.js_Template Engine_Underscore.js Templating - Fatal编程技术网

Javascript Underline.js模板IE9/IE10未返回正确的代码

Javascript Underline.js模板IE9/IE10未返回正确的代码,javascript,internet-explorer,underscore.js,template-engine,underscore.js-templating,Javascript,Internet Explorer,Underscore.js,Template Engine,Underscore.js Templating,我的js文件中有这个代码 var selectedTagElement = _.template('$("#selected_tag_item_template").html()', item = { tag: label, value: value }); $('#wrapper_id').append(selectedTagElement); 这在我的html文件中 <script type="text/template" id="selected_tag_item_tem

我的js文件中有这个代码

var selectedTagElement = _.template('$("#selected_tag_item_template").html()', item = { tag: label, value: value });
$('#wrapper_id').append(selectedTagElement); 
这在我的html文件中

    <script type="text/template" id="selected_tag_item_template">
      <div class="tag_item selected js-selected_tag_item" 
           data-tag-value="<%= item.value %>" 
           data-tag-label="<%= item.tag %>"><%= item.tag %>
      </div>
    </script>
我得到的只是

LOG: function(n){return o.call(this,n,m)} 
如果我试图打印html文件中的项目变量,就像这样

<%= item %>

怎么了?感谢您从以下方面获得的输出:

console.log(selectedTagElement)
指示
selectedTagElement
是一个函数。过去,您可以一步编译并填写模板,但事实并非如此

您需要分两步开始构建
selectedTagElement

  • 使用
    将模板编译为函数.template
  • 使用所需数据运行该函数以获取HTML
  • 所以你想说:

    var t = _.template($('#selected_tag_item_template').html());
    var selectedTagElement = t({ item: { tag: label, value: value } });
    
    这应该适用于任何地方,并且符合标准用法


    你的代码在工作的地方是偶然的。我将假设
    “$(“#js_template”).html()”
    只是一个输入错误,因为它在其他方面没有意义。让我们将您的代码分解为等效的代码:

    item = { tag: label, value: value };
    var selectedTagElement = _.template('$("#js_template").html()', item);
    $('#wrapper_id').append(selectedTagElement); 
    
    第一行创建一个全局
    变量,该变量保存要提供给模板的数据

    第二行将把模板编译成一个函数,并完全忽略第二个
    item
    参数,因为它与的任何选项都不匹配

    第三行将一个函数传递给
    [append
    ]。通常你会给
    append
    一个HTML字符串或DOM节点,但你也可以给它一个函数;当您给
    append
    一个函数时,它运行该函数并将其返回值用作HTML字符串。在非IE浏览器中,模板函数将通过意外全局变量获取

    item = { tag: label, value: value }
    
    但是IE正在某处使用
    item
    函数(在浏览器中使用本机代码实现),而不是使用
    item
    变量。

    是的,对不起,#js_包装器是一个打字错误。我纠正了它。谢谢,它很管用!
    item = { tag: label, value: value };
    var selectedTagElement = _.template('$("#js_template").html()', item);
    $('#wrapper_id').append(selectedTagElement); 
    
    item = { tag: label, value: value }