2009-11-18 46 views
0

我知道,如果你使用Ajax的表單元素加載一個div,那麼你必須使用實時功能添加事件到那些不在DOM樹中的元素。 ...jQuery,Ajax和生活功能問題

同時,我在jQuery的網站,居住功能目前不支持重點讀,模糊等....

我應該怎麼做才能調用函數時的元素通過加載到一個div阿賈克斯是集中還是模糊......?

是否應該用綁定來完成......?但談論綁定,即使活着和綁定看起來有點相似,但它不能用於上述場景......正確......?

,並在這裏不用代碼....

<BODY style=""> 

    <div style="margin-top:5px; width:100%" class="subAndContent" id="subAndContent"> 
     <!-- TABLE TO HOLD SUB MENU--> 
     <div id="subMenuDiv"> 
      <table width="100%" > 
       <tr align="center" valign="middle"> 

        <td width="100%" valign="middle" class="rounded" > 

         <div class="sidebarmenu"> 
          <ul id="sidebarmenu1"> 
           <li> 
            <a href="javascript:ajaxLoadMainOnly('createHotel.php', 'content')" > <!-- This function get's the page to be loaded and the div into which it should be loaded and uses ajax to do the loading... --> 
             HOTEL 
            </a> 
           </li> 
           <li> 
            <a href="javascript:ajaxLoadMainOnly('createCountry.php', 'content')" > 
             COUNTRY 
            </a> 
           </li> 

           <li> 
            <a href="javascript:ajaxLoadMainOnly('createCity.php', 'content')"> 
             CITY 
            </a> 
           </li> 
          </ul> 
         </div> 

        </td> 
       </tr> 
      </table> <!-- END TABLE TO HOLD SUB MENU--> 
     </div> 

     <div id="contentDiv" class="rounded"> 
      <!-- TABLE TO HOLD CONTENT PANE--> 
      <table width="100%" style="float:left;"> 
       <tr valign="left"> 
        <td align="center"> 
         <div id ="content"> 
          <!-- Div into which the content will be loaded --> 
         </div> 
        </td> 
       </tr> 
      </table> 
     </div> 
    </div> 

    <!-- DIV AND TABLE TO HOLD FOOTER --> 
    <?php 
    include 'footer.php'; 
    ?> 


</BODY> 
+5

你是否從短信中發短信? :-) – Nosredna 2009-11-18 04:41:51

+0

不,我沒有....錯了......? – SpikETidE 2009-11-18 04:42:51

+1

閱讀所有的「u」和「r」並且沒有大寫字母有點難。 :-) – Nosredna 2009-11-18 04:45:07

回答

1

您必須獲取動態加載的元素,然後使用bind添加焦點和模糊處理程序。例如,如果您希望將處理程序添加到帶班「LONGTEXT」一個文本,你可以使用這樣的:

$.getJSON('createHotel.php', { /* params go here */ }, receiveHotelCreated); 

function receiveHotelCreated(data) { 
    $('#content textarea.longtext').bind('blur', onTABlur); 
} 

function onTABlur() { 
    var ta = $(this); // The textarea on which the blur event occurred 
    alert('Textarea blurred'); 
    // Your code goes here 
} 

的onTABlur(TA =文本區)的功能是當焦點離開被稱爲textarea的。在函數內部,this引用該函數被調用的元素。當我們收到AJAX響應(receiveHotelCreated)時,我們將它綁定到文本區域,這將成爲所需的文本區域元素。我們在jQuery函數($(this))中包裝this以獲得一些額外的功能。

+0

感謝您的建議... 我會嘗試他們兩人,並儘快回覆您... – SpikETidE 2009-11-18 05:31:40

+0

嗨湯姆, 可以請詳細說明onTABlur函數,特別是$(e.target)... ..? – SpikETidE 2009-11-18 12:49:03

+0

還記得你答應寫「你」而不是「你」;-)我刪除了'$(e.target)',因爲你並不真正需要它,我想這有點令人困惑。關於'onTABlur()'的具體不清楚? – 2009-11-18 13:54:43

0

我相信,我讀了jQuery的下一個版本(1.4)覆蓋與直播的剩餘事件。

但現在,用1.3,你需要使用綁定(或像「點擊」的快捷方式)。是的,如果你添加元素到頁面,Live不會爲你正在做的工作,你需要綁定添加的內容。

documentation for "Live"是一個很好的開始。我認爲如果你想處理Live尚未處理的事件,你仍然可以使用liveQuery插件。

+0

請你指點一些例子......?我的印象是,綁定不會做我想做的...... – SpikETidE 2009-11-18 04:46:21

+0

我以爲綁定不會這樣做,因爲我不認爲它會分別裝載了ajax的元素... – SpikETidE 2009-11-18 04:48:07

+0

你有任何代碼,所以我可以看到什麼不適合你?我不確定你是如何加載新內容的。 – Nosredna 2009-11-18 04:51:56

0

這裏有一個簡單的例子:

這解除綁定並重新綁定每次調用Ajax請求的時間焦點事件。解綁是爲了安全,並確保沒有任何剩餘事件。

$(document).ready(function() { 
    focusEventFunction(); 
    ajaxLoader(); 
} 

function ajaxLoader(); 

    $('#sidebarmenu1 a').unbind().bind('click', function(event){ 

    $.get('url_to_get.php', function(data) { 

     // CODE THAT REPLACES DIVS AND DATA 

     //The following has to be inside the ajax callback and 
     //after the code that changes divs. this will remap all the functions 
     //that bind the focus. 

     $('selector_for_elements_needing_focus').unbind().bind('focus', function(event){ 
      focusEventFunction($(this), event) 

      ajaxLoader(); //this ensures your ajax gets called again, 
          //depending on the complexity of the html changes 
          //you may not need this. 
     } 
} 


function focusEventFunction(jElement, event) { 
    //just a dummy function that does your focus/blur stuff. 
    // you might not need the parameters 
}