- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 mustache.js 作为我的客户端模板解决方案,但我遇到了一个问题,我不知道为什么会这样:
首先,这是我的模板:
<ul>
{{#steps}}
<li>
<a href="{{url}}">
<div class="step_visual {{step_status}}">
<span class="step_description">{{description}}</span>
</div>
</a>
</li>
{{/steps}}
</ul>
这是我要传入的 json 对象:
var view = {
"steps": [
{
"url": "complete_profile",
"step_status": "done",
"description": "Complete Proflie"
},
{
"url": "complete_form1",
"step_status": "active",
"description": "Submission of Form 1"
}
]
}
我尝试运行此脚本以在我的应用程序中呈现它
var content = Mustache.render(template, view);
$(target).html(content).hide().fadeIn();
其中 template
= 上面的模板 |view
= View 对象target
是我希望更改内容的 html 元素
让我感到困惑的是,当我的模板看起来像这样时它工作得很好
{{#steps}}
{{url}}
{{description}}
{{step_status}}
{{/steps}}
或者如果我的模板没有任何循环。
可能是什么错误?你认为这是 mustache 的错误吗?
编辑正如我所见,代码有效,
会不会是我调用函数的方式有问题?
tmplReplaceContent : function(json, tmpl, target) {
var regex = new RegExp("\{{[a-zA-Z\.\_]*\}}"),
view = '';
function setOutput(template) {
var content = Mustache.render(template, view);
$(target).html(content).hide().fadeIn();
}
function doJSON(json) {
view = json;
if(!regex.test(tmpl)){
/* get mustache tmpl from the path */
$.get(msi.vars.tmpl_url + tmpl + '.mustache', setOutput);
} else {
setOutput(tmpl);
}
}
/* json -> check if object */
if (typeof json === 'object') {
doJSON(json);
} else {
/* getJSON from the path */
$.getJSON(msi.vars.base_url + json, doJSON);
}
}
最佳答案
我从来没有这种如释重负的感觉,因为我自己解决了我的问题,首先感谢Tomalak感谢您持续关注我的评论并帮助我解决问题。
如果我继续使用 console.logging 东西,我本可以更早地解决问题,但我停了下来。
我注意到的一件事是 $.get
功能给我#<Document>
,这几乎是一个对象,我可以早先推断出响应是一个对象,从我的问题的标题来看,显然它正在返回一个对象。
那么为什么不添加 dataType
属性?
function doJSON(data){
dataSource = data;
if (!regex.test(tpl)) {
$.get(msi.vars.tmpl_url + tpl + '.mustache', render, 'text'); // see here
} else {
render(tpl);
}
}
这最终解决了问题。
关于javascript - Mustache.js 对象 #<Document> 没有方法 'search',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14004605/
我正在尝试计算 iFrame 的高度,但不明白为什么 document.body.offsetHeight + document.body.bottomMargin 不等于 document.docu
我正在使用 Node/Mongoose/MongoDB 并尝试构建一个轮询应用程序。一个关键需求是跟踪单个用户对同一民意调查的响应如何随时间变化(他们一遍又一遍地进行同一民意调查)。 我有一个用户模型
首先,我不是普通的博主,我很困惑。如果我的问题不符合要求,请指导我。我会努力改进的。 我已提交 Microsoft Code Review 的 Microsoft CRM 插件。我是 JavaScri
谁能解释为什么使用类似的东西: gci -force "\\computername\c$\users\username\Documents" -recurse 或者 gci -force "\\co
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, over
这个问题已经有答案了: What is the (function() { } )() construct in JavaScript? (28 个回答) 已关闭 6 年前。 说实话,一开始我以为我可
document.getElementsByTagName("*") 适用于 IE/Firefox/Opera,但不适用于 Chrome 和 Safari。 document.all 适用于 IE/C
这个问题在这里已经有了答案: What is the difference between Document and document in JavaScript? (2 个答案) 关闭 8 年前。
我以某种方式发现将事件监听器添加到文档的行为有点奇怪。虽然向 HTMLElements 添加监听器工作正常,但向文档添加监听器不起作用。但奇怪的是,使用 jQuery 可以让它工作。 那么有人可以解释
谁能告诉我这两个 JavaScript 命令之间的区别? 这两个跨主要浏览器的兼容性是什么?我知道 documentElement 与大多数浏览器兼容。 谢谢 最佳答案 document.docume
什么时候应该使用 document.all 与 document.getElementById? 最佳答案 document.all 是 Microsoft 对 W3C 标准的专有扩展。 getEle
当升级到 react-native 0.61.2 时,这个问题出现了。我做到了从手机中删除了 apk 和自动链接使用 react-native link 然后 react-native run-and
当升级到 react-native 0.61.2 时,这个问题出现了。我做到了从手机中删除了 apk 和自动链接使用 react-native link 然后 react-native run-and
我将收到 tungstenite::Message ,它将包含来自客户端的bson文档。我可以将tungstenite::Message转换为Vec,但是如何在服务器端将其转换回 bson::docu
我这里有一个简单的疑问: 文档对象范围位于浏览器选项卡内:我的意思是如果我设置document.tab1 ='tab1' 在一个浏览器选项卡中 它在其他选项卡中不可用。 但是 document.coo
我经常使用并看到推荐的 dom 访问结构,例如这样动态地将内容添加到页面: loader = document.createElement('script'); loader.src = "myurl
我对 JQuery 还很陌生。我正在使用this JQuery 函数在元素上显示工具提示。 我根据我的需要(在这个社区的帮助下)以这种方式编辑了代码: $(document).ready(functi
我想知道哪个是运行js代码的正确方法,该代码根据窗口高度计算垂直菜单的高度并按时设置,不晚不早。 我正在使用 document.ready 但它并没有真正帮助我解决这个问题,它有时没有设置,我必须重新
我正在浏览一个 js 文件并发现这个声明var dataobj=document.all? document.all.id_name : document.getElementById("id_nam
想知道何时使用,这适用于什么浏览器? if (document.all&&document.getElementById) { // Some code block } 最佳答案 我认为没有任何重要的
我是一名优秀的程序员,十分优秀!