单击事件在触发一次主干js后停止工作
Click events stop working after firing once backbone js
我有很多点击事件,它们链接到我网站上的不同位置以及我网站外的页面。
这似乎是我第一次点击一个,它最初是有效的,但在那之后,我所有的点击事件都完全停止了。
不确定发生了什么,我应该发布什么代码?
我正在使用路由器来渲染网站上的一些视图。
我还应该提到,我的控制台没有显示任何错误。
这里是脚本的调用位置。点击事件的id已经编辑过了,但我已经检查过了,都是正确的(点击第一次就可以工作,所以它们无论如何都会是正确的)。
EDIT重要提示:在出现导航之前,点击事件似乎是正常的
<!--templates-->
<!--Home -->
<script type="template/jquery" id="home_template">
<%= partial "templates/home_template" %>
</script>
<!--Portfolio -->
<script type="template/jquery" id="portfolio_template">
<%= partial "templates/portfolio_template" %>
</script>
<!--About-->
<script type="template/jquery" id="about_template">
<%= partial "templates/about_template" %>
</script>
<!--Javascripts-->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>
<script src="https://cdn.firebase.com/v0/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/backfire/0.3.0/backbone-firebase.min.js"></script>
<script src="javascripts/modernizr.js"></script>
<!--Application-->
<script src="javascripts/models.js"></script>
<script src="javascripts/views.js"></script>
<script src="javascripts/routes.js"></script>
<!--script src="javascripts/application.js"></script-->
<script>
$(document).ready(function () {
//home links
$("#recent_work").click(function() {
router.navigate("portfolio", true)
});
//portfolio links
$("#xx").click(function() {
window.open('http://chexxxxxs.com.au/');
});
$("#xx").click(function() {
window.open('http://updaxxxxxal.com.au/');
});
$("#xx").click(function() {
window.open('http://whxxxxnry.com.au/');
});
$("#xx").click(function() {
window.open('http://frexxxxxe.com.au/');
});
$("#xx").click(function() {
window.open('http://puxxxxxel.com/');
});
$("#xx").click(function() {
window.open('http://xxxxxxing.com.au/');
});
});
</script>
路由文件:
var Router = Backbone.Router.extend({
routes: {
'': 'home',
'home' : 'home',
'portfolio' : 'portfolio',
'about' : 'about'
}
});
var homeView = new HomeView({ el: $("#container") });
var portfolioView = new PortfolioView({ el: $("#container") });
var aboutView = new AboutView({ el: $("#container") });
var router = new Router();
router.on('route:home', function () {
homeView.render();
});
router.on('route:portfolio', function () {
portfolioView.render();
});
router.on('route:about', function () {
aboutView.render();
});
Backbone.history.start();
视图:
var HomeView = Backbone.View.extend({
initialize : function () {
this.render();
},
render : function () {
var template = _.template( $("#home_template").html(), {} );
this.$el.html(template);
}
});
var PortfolioView = Backbone.View.extend({
initialize : function () {
this.render();
},
render : function () {
var template = _.template( $("#portfolio_template").html(), {} );
this.$el.html(template);
}
});
var AboutView = Backbone.View.extend({
initialize : function () {
this.render();
},
render : function () {
var template = _.template( $("#about_template").html(), {} );
this.$el.html(template);
}
});
感谢@undefined提供的帮助。
当我的视图被呈现时,容器被.html()
清空,这导致事件的解除绑定。在主干网中,视图中的事件必须在视图本身的"事件"散列中定义,当您执行此主干网时,会自动重新委派事件。
var PortfolioView = Backbone.View.extend({
initialize : function () {
this.render();
},
render : function () {
var template = _.template( $("#portfolio_template").html(), {} );
this.$el.html(template);
},
events: {
"click .portfolio_item" : "linktoLive"
},
linktoLive: function (e) {
var link = $(e.currentTarget).attr("data-link");
window.open(link);
}
});
在事件散列下,以event binding : function to execute on event
格式添加事件,然后添加后面的函数。
相关文章:
- 根据Angular.JS上一次的内容禁用选择
- 更改路由后,运行一次js脚本
- 我可以使用JS一次使用JSON数组中的一个字符串吗
- mongoose.js Model.remove在循环中只能工作一次
- Angular.js指令格式化程序只调用过一次,或者如何告诉ngModel表现得像watchCollection
- data th JS每页只工作一次
- 如何每 10 秒调用一次 JS 函数,然后以角度激活一个函数
- 节点 js 在启动时调用函数一次
- 您可以在节点.js上一次运行多个应用程序吗?
- 只加载一次js函数
- 如何使网页一次只能访问一次?(JS+HTML)
- 如何在不刷新页面的情况下每 10 秒在本地重新加载一次 JS 文件
- 只运行一次js文件
- 在多次激发时运行一次js函数
- 如何为可以多次加载的ajax页面处理只加载一次js
- 如何只显示一次JS警报
- 每次访问只执行一次js函数,刷新时再次执行
- 如何使用cookie只执行一次JS脚本
- 调用一次js函数后页面将冻结
- 如何在Phonegap中只加载一次js库