在Handlebars中使用JS
Using JS In Handlebars
本文关键字:JS Handlebars 更新时间:2023-09-26
我使用手把在一个简单的移动网页上使用swipejs模板。我制作了一个没有模板引擎的网页原型,效果很好。然后,我研究了网络,寻找一个好的模板引擎,并找到了把手。
我把网页的正文嵌入到一个脚本标签中,这样车把就可以工作了。除了swipejs(我用它来处理图像)之外,一切都很好。
以下是我的HTMl:的摘要版本
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=no" />
<link media="Screen" href="styles.css" type="text/css" rel="stylesheet" />
<link media="handheld, only screen and (max-width: 480px), only screen and (max-device-width: 480px)" href="css/mobile.css" type="text/css" rel="stylesheet" />
<script src="scripts/jquery.js"></script>
<script src="scripts/handlebars.js"></script>
<script src="scripts/app.js"></script>
<script src='scripts/swipe.js'></script>
<!--[if IEMobile]>
<link rel="stylesheet" type="text/css" href="mobile.css" media="screen" />
<![endif]-->
</head>
<body>
<script id="description-script" type="text/x-handlebars-template">
<h1>Main Title</h1>
<div id="pictures">
<div id="slider" class="swipe">
<div class="swipe-wrap">
<div><center><img style="width:95%;height:auto;" src="image link 1"/></center></div>
<div><center><img style="width:95%;height:auto;" src="image link 2"/></center></div>
<div><center><img style="width:95%;height:auto;" src="image link 3"/></center></div>
</div>
</div>
</div>
<h2>Description</h2>
<div id="description">
{{description}}
</div>
</script>
<script>
//swipe script
window.mySwipe = Swipe(document.getElementById('slider'),
{
auto: 3000,
speed: 300
});
</script>
</body>
提前感谢您的帮助。我是否必须使用不同的模板引擎,或者这里有什么东西我忘记了?
您可能需要在模板渲染后绑定滑动事件。或者尝试将其绑定到整个"body"元素,而不是#slider。
document.getElementById('slider')
将失败,因为它将返回null,而滑块位于以下脚本标记中
<script id="description-script" type="text/x-handlebars-template">
在app.js
中呈现description-script
模板后,您需要立即将Swipe(...)
代码移动到。
类似于:
// Compile the template
var template = Handlebars.compile($('#description-script').html());
// Render the template
// This makes the #slider DIV appear in the DOM
var data = { description: 'Photos' };
$('body').append(template(data));
// Start Swipe
window.mySwipe = Swipe($('#slider').get(0), {
auto: 3000,
speed: 300
});
请参阅以下JSFiddle(从@TommyBs派生):http://jsfiddle.net/b4ta3/2/
相关文章:
- Backbone.js&Handlebars.js来创建相对URL
- 在Handlebars.js中的新行上打印输出
- 递归Handlebars.js模板.如何确定深度
- 如何将这个Json映射到Handlebars js
- 获取模板是用我的Node.JS应用程序的新版本Handlebars错误预编译的
- 如何使用Handlebars.js按状态拆分对象数组
- Handlebars js未在对象上迭代
- 使用Handlebars.js重新呈现HTML中的变量
- 在Meteor.JS Handlebars中将对象转换为数组后无法拆分字符串
- Handlebars.js打印对象没有'不起作用
- Handlebars.js和链接标记
- 在Handlebars.js中查询块帮助程序
- 使用Require.js未定义Handlebars
- 在 Handlebars.js 中呈现部分时向上下文添加属性
- 如何使用 Handlebars.js 访问 JSON 对象
- 使用 Handlebars.js 处理 executeSql 语句的结果
- 如何在 Node.js 中使用 Express、Handlebars 和 Conslidate 设置部分的路径
- 如何在 Handlebars.js 模板上绑定数据以进行自动值更新
- Node.js与服务器和客户端上的Handlebars.js
- Ember.js Handlebars BoundHelper Uncaught TypeError: Object #