2014-01-26 64 views
0

我有一個問題,使用backbone.js,這是我的代碼。骨幹視圖無法運行clickAction方法

<div id="dx-container"> 
    <aside id="dx-sidebar"> 
     <button>SLIDE</button> 
    </aside> 
    <section id="dx-content"> 
     CONTENT CONTENT<br/> 
    </section> 
</div> 

<script type="text/javascript" src="js/library/jquery/jquery-2.0.2.min.js"></script> 
<script type="text/javascript" src="js/library/underscore/underscore-min.js"></script> 
<script type="text/javascript" src="js/library/backbone/backbone-min.js"></script> 
<script> 
    var View = Backbone.View.extend({ 
     el: '#dx-content', 
     initialize: function() { 
      this.heloo(); 
     }, 
     events: { 
      "click button": "clickAction" 
     }, 
     heloo: function(){ 
      this.$el.html("Hello World"); 
      console.log("Hello World Sucess"); 
     }, 
     clickAction: function(e){ 
      this.$el.html("Click Action Success"); 
      console.log("Click Action Success"); 
     } 
    }); 
    var tes = new View(); 
</script> 

當按鈕操作點擊,骨幹視圖不能運行clickAction方法,但如果使用構造這種方法來看,這種可以運行,誰能幫助我,謝謝。 enter image description here

+2

事件代表團在'#DX-content'範圍做了,而且也沒有按鈕。使範圍更寬,將其設置爲'#dx-container'或使用'$('#dx-sidebar button')。on('click',fn)'或其他方法處理單擊。 –

回答

0

電話需要是#DX-容器上:

var View = Backbone.View.extend({ 
    el: '#dx-container', 
    initialize: function() { 
     this.heloo(); 
    }, 
    events: { 
     "click button": "clickAction" 
    }, 
    heloo: function(){ 
     this.$el.html("Hello World"); 
     console.log("Hello World Sucess"); 
    }, 
    clickAction: function(e){ 
     this.$el.html("Click Action Success"); 
     console.log("Click Action Success"); 
    } 
}); 
var tes = new View(); 
+0

感謝您的幫助 – denyptw

0

我可以用它到底是什麼傻冒要求混爲一談,但是從我離開你的問題是,當你在你的編輯器中查看你的頁面一切正常,但一旦上傳到互聯網,它停止工作?如果這是你的意思,那很可能是src=""問題。

例如: 當您從編輯器查看您的頁面時,它可以引用您的腳本文件,因爲它們與您的頁面一起位於您的計算機上。所以,這會很好。

<script type="text/javascript" src="js/library/jquery/jquery-2.0.2.min.js"></script> 
<script type="text/javascript" src="js/library/underscore/underscore-min.js"></script> 
<script type="text/javascript" src="js/library/backbone/backbone-min.js"></script> 

然而,一旦你上傳你的網頁,你也有你的.js文件上傳到的確切路徑,它顯示以上或只是確切知道位置替換路徑:

<script type="text/javascript" src="http://www.yoursite.com/path/to/jquery-2.0.2.min.js"></script> 
<script type="text/javascript" src="http://www.yoursite.com/path/to/underscore-min.js"></script> 
<script type="text/javascript" src="http://www.yoursite.com/path/to/backbone-min.js"></script> 

如果這不是你要求的,我很抱歉,這只是有點不清楚。

0

感謝您的幫助,按鈕必須是#DX-容器

var View = Backbone.View.extend({ 
    el: '#dx-container', 
    initialize: function() { 

    }, 
    events: { 
     "click button": "clickAction" 
    }, 
    clickAction: function(){ 
     this.$el.append("Click Action Success"); 
    } 
}); 
var tes = new View();