2012-01-19 152 views
2

我使用的Symfony 2,我需要執行下面的樹枝模板加載剛過一個javascript:執行JavaScript加載

<div data-role="dialog"> 
    <div data-role="header"> 
     <h1>Input your Username and Password</h1> 
    </div> 
    <div data-role="content"> 
      <div data-role="fieldcontain" class="ui-hide-label"> 
       <label for="username">Username:</label> 
       <input type="text" name="username" id="username" value="" placeholder="Username"/> 
      </div> 
      <div data-role="fieldcontain" class="ui-hide-label"> 
       <label for="password">Password:</label> 
       <input type="password" name="password" id="password" value="" placeholder="password" /> 
      </div> 
      <div data-role="controlgroup" data-type="horizontal" align="center"> 
       <input type="button" value="Login"/> 
       <a data-rel="back" data-role="button"/>Cancel</a> 
      </div> 
    </div> 
</div> 
+2

首先,我沒有在這裏看到任何樹枝變量! 。並且關於加載部分,在服務器端渲染了小枝模板,然後將呈現的html發送到客戶端。所以如果你想在你的模板加載完成後執行你的JS,我假設你的意思是說在頁面加載後?爲此,您只需添加 '' 您的html後,並鏈接頁面加載事件的代碼。 – satin

+2

這與Twig沒有任何關係。 – richsage

回答

1
<script type="text/javascript"> 
     $("#divId").live('pageinit', 
     function(event) 
     { 
     }); 
</script> 

通常,當我們使用jquery mobile時,一個頁面的主容器是一個帶有數據欄的div,爲這個div設置一個id,並用上面腳本中的divid替換它,並獲得你的代碼工作:-)

5

在模板的底部添加

<script type="text/javascript"> 
    window.onload = function() { 
     //do your stuff here 
    } 
</script> 

或者,如果你正在使用jQuery ..

<script type="text/javascript"> 
    $(function() { 
     //do your stuff here 
    }); 
</script> 
+0

什麼不起作用?如果你查看頁面源代碼是否在那裏? – JamesHalsall

+0

當使用jquery mobile時,頁面之間的切換是不同的,但是非常感謝,我設法讓它工作,我發佈了答案:) – Noor