简体   繁体   English

jQuery click函数和回调数组

[英]jQuery click function and Callback Arrays

I`m new to jQuery and have a problem understanding functions. 我是jQuery的新手,对函数的理解有问题。

I have the following structure of HTML code: 我具有以下HTML代码结构:

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

And this jQuery code: 和这个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(''); //???
            });
        });
    });
});

Now my question is what $('.result').html(''); 现在我的问题是什么$('.result').html(''); does? 有吗

JQuery .html() property sets the html of the object it is mapped to, it have same behavior like the javascript property .innerHTML. JQuery .html()属性设置它映射到的对象的html,它具有与javascript属性.innerHTML相同的行为。

So here in your scenario $('.result').html(''); 因此,这里是您的情况$('。result')。html(''); will set the html of result class element to null. 将结果类元素的html设置为null。

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

Secondly, you are using wrong approach in your 'file.php', instead use this code: 其次,您在'file.php'中使用错误的方法,而是使用以下代码:

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

$('.result').html(''); sets the html of .result to a blank string. .result的html设置为空白字符串。

$('.result').html(''); clears the contents of <ul class="result"> . 清除<ul class="result">

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM