简体   繁体   English

没有触发中枢事件,我也不明白为什么

[英]backbone.js events not firing and I don't understand why

Based off of I dont know how many examples. 基于我不知道有多少例子。 This should work, yet it does not. 这应该可以,但是不能。 I am so new to backbone.js that this just hurts my head cause I can write jquery based code with my eyes close, I can write native javascript no problem. 我对bone.js太陌生,以至于这只会伤到我的头,因为我可以闭上眼睛编写基于jquery的代码,我可以编写本机javascript没问题。 But this, what? 但是,这是什么? Why? 为什么? the click will not work, the "events" as far as I can tell just don't register. 点击将不起作用,据我所知,“事件”只是不进行注册。 Can I not do event's on divs? 我不能在div上进行活动吗? or what am I missing? 还是我想念什么?

Now I know this will be flagged a duplicate, I know people will just link to some other example. 现在我知道这将被标记为重复,我知道人们将链接到其他示例。 The problem is I've likely already seen it, compared it, and still for the life of me can't figure out what is wrong. 问题是我可能已经看过,比较过它,但对我一生仍然无法弄清楚哪里出了问题。 It works up until the events, when I click on the div I'd assume the event be fired and its not. 它可以一直工作到事件发生,当我单击div时,我会假定事件被触发而事件未被触发。 This is my issue I don't know why, and not knowing why is whats causing the headache 这是我的问题,我不知道为什么,也不知道为什么会引起头痛

<div id="test"></div>
<div>Test</div>

        <script>

            var BodyView = Backbone.View.extend({
              el: '#test',
              events:{
                  "click #test"         : "changeColorOne"
              },
              changeColorOne: function()
              {
                  console.log('hate');
                  $(this.el).css({color:'#00F'});
              },
              changeColorTwo: function()
              {
                  $(this.el).css({color:'#000'});
              },
              render: function(str)
              {
                 $(this.el).html('Hello').css({color:'#F00'});
                 $(this.el).append(' World');
              },
              initialize: function()
              {
                  this.render();
              }


            });

            var body = new BodyView();

            console.log(body.el);
        </script>

click #test is trying to attach to $('#test').find('#test') which will be empty. click #test尝试将其附加到$('#test').find('#test')并将其为空。

Either pick an element inside your container or remove #test to attach to the container. 在容器中选择一个元素,或删除#test以附加到容器。

el: '#test',
events:{
  "click" : "changeColorOne"
}

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM