[英]How to call jQuery function inside Handlebar script (template) tag?
車把腳本標簽為:
<section class="listtext">
<div class="container">
<p class="test">hello</p>
<div>
</section>
我的jQuery是:
$(document).ready(function(){
$("p").click(function(){
alert("Alert clicked.");
});
});
請幫助在句柄中的script標簽內運行此jQuery。
您可以定義一個組件:
/templates/components/my-hello.hbs
<section class="listtext">
<div class="container">
<p class="test">hello</p>
<div>
</section>
/components/my-hello.js
import Ember from 'ember';
export default Ember.Component.extend({
didInsertElement () {
this.$("p").click(function(){
alert("Alert clicked.");
});
}
});
之后,無論何時插入此組件。 jQuery代碼段將在組件的根元素范圍內執行。
更新
如果您不想使用Ember,我看到的唯一方法是使用dom選擇器:
$("body").on("click", ".listtext p", function(){
alert("Alert clicked.");
});
您必須將jQuery代碼放到smirnov建議的組件中,或放到didInsertElement鈎子的站點視圖中,但是從ember 1.13.8開始不推薦使用這些視圖。
因此,我建議您使用smirnov提供的解決方案! :-)
最好的祝福
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.