javascript - 此代码的工作原理 .html(_.template()({ }) )

标签 javascript jquery underscore.js

我正在阅读一个应用程序的源代码,我看到了这行代码:

 $('#div1').html(_.template($('#div2').html())({Id: app.Id(id)}));

我能理解$('#div1').html(),但是为什么这行代码可以传递两个()代码块呢?看起来不对劲。 .html() 可以带两个 () block 吗?

.html(_.template()());

最佳答案

这是因为 _.template() 返回一个函数,然后我们使用第二组 () 调用该返回的函数

var fn = _.template(sometemplate);//it gives a parsed template which is a function
fn(data);//it merges the data and the template to generate the html

关于javascript - 此代码的工作原理 .html(_.template()({ }) ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18371240/

相关文章:

javascript - 带计数的组数组

javascript - 在javascript中将数字美化为最接近的数字,末尾加零

javascript - 如何在js中打包无符号整数?

javascript - 如何使用 intro.js 突出显示 Bootstrap 模式

javascript - Unicode 三 Angular 自旋

jquery - 我想根据我的段落调整我的标题。是否可以?

php - 我的 php 和 jQuery Ajax 更新不起作用

javascript - 阻止加载 DOM 对象

javascript - 允许元素的前后内容覆盖父溢出限制

javascript - 如何正确使用_.omit