2013-04-17 182 views
0

我是jQuery的新手,並且有理解功能的問題。jQuery點擊功能和回調數組

我的HTML代碼的結構如下:

<ul class="result"> 
    <li id="a"></li> 
    <li></li> //will be added through a loop depending on input 
</ul> 

這jQuery代碼:

$(document).ready(function() { //changes are possible when the page has fully loaded 
    $('.inp').keyup(function() { //executes keyup function to the field class "inp" 
     var inp = $(this).attr('value'); //sets a variable with the value of input from class="inp" 
     $.post('ajax/file.php', {inp:inp}, function(data) { //posts that value to file.php as variable inp and executes the function (data) to this 
      $('.result').html(data); //adds the returned result into HTML content as a first element in the set of class="result" => <li> 

      $('.result li').click(function() { //executes click function when clicking li element 
       var result_value = $(this).text(); //sets a variable and prepares something as text 
       $('.inp').attr('value', result_value); //fills value attribute of field class "inp" with the variable result_value 
       $('.result').html(''); //??? 
      }); 
     }); 
    }); 
}); 

現在我的問題是什麼$('.result').html('');呢?

+0

'$( '的結果。 ')HTML('');',設定的的''.result'到ul'空白的HTML,它刪除的一切,就是'ul'內 –

+0

'$('。result')。html(''); '設置與類結果以空白標籤的內部HTML,我想是將使'

    '從現有'
  • //將通過一個循環中加入根據輸入 '。請參閱http://api.jquery.com/html/#html2 – dreamweiver

    回答

    1

    JQuery .html()屬性設置它映射到的對象的html,它具有與javascript屬性.innerHTML相同的行爲。

    因此,在你的場景中$('。result')。html('');將結果類元素的html設置爲null。

    <ul class="result"> 
    </ul> 
    

    其次,您使用的是錯誤的做法,在你的 'file.php',而不是使用此代碼:

    echo '<li>'.$row["name_1"].'</li>'; 
    echo '<li>'.$row["name_2"].'</li>'; 
    echo '<li>'.$row["name_3"].'</li>'; 
    
    0

    $('.result').html('');.result的html設置爲空字符串。