jQuery点击函数和回调数组


jQuery click function and Callback Arrays

我是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('');做什么?

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>';

$('.result').html('');设置.result的html为空字符串

$('.result').html('');清除<ul class="result">的内容

http://api.jquery.com/html/