2014-03-28 28 views
0

我需要知道如何在Meteor中使用Jquery框架。我使用Jquery按鈕事件做了一個簡單的例子,但得到了一些錯誤。我對這個錯誤沒有任何想法。所以請看下面的代碼,並告訴我該怎麼做?在流星中使用jquery的按鈕事件問題?

HTML代碼:

app.html 
-------- 

<head> 
    <title>app</title> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
</head> 

<body> 
    {{> menu}} 
</body> 

menu.html 
--------- 

<template name="menu"> 

<h2>This is a heading</h2> 
<p>This is a paragraph.</p> 
<p>This is another paragraph.</p> 
<button>Click me</button> 

</template> 

JS代碼:

if (Meteor.isClient) 
{ 
Template.menu.events 
    ({ 
     $(document).ready(function() 
     { 
      $("button").click(function() 
      { 
      $("p").hide(); 
      }); 
     }); 
     }); 
    } 

錯誤消息:

​​
+0

1)這是編譯錯誤,2)不要手動加載jquery - meteor包括它,3)不要在流星應用中使用'$(document).ready' - 用事件地圖替換此代碼。 –

回答

2

參見meteor event maps documentation用於Template.menu.events的選項的示例()。你點擊功能可以這樣寫:

Template.menu.events({ 
    'click button': function(){ 
    $("p").hide(); 
    } 
}); 

如果你需要使用jQuery來添加一個事件,一個更好的地方是在渲染功能的模板。像這樣:

Template.menu.rendered = function(){ 
    $("button").click(function(){ 
    $("p").hide(); 
    }); 
}; 
+0

好的,謝謝你的工作正常,但在Web控制檯出錯。所以你可以請檢查一次錯誤:'[19:10:05.567]解析'背景'的值時出錯。聲明下降了。 @ http:// localhost:3000 /。「。@ user728291 – Venkat