dongmiao260399 2013-04-17 09:43
浏览 53
已采纳

jQuery单击函数和回调数组

I`m new to jQuery and have a problem understanding functions.

I have the following structure of HTML code:

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

And this jQuery code:

$(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(''); does?

  • 写回答

3条回答 默认 最新

  • doushangxianq07480 2013-04-17 10:02
    关注

    JQuery .html() property sets the html of the object it is mapped to, it have same behavior like the javascript property .innerHTML.

    So here in your scenario $('.result').html(''); will set the html of result class element to null.

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

    Secondly, you are using wrong approach in your 'file.php', instead use this code:

    echo '<li>'.$row["name_1"].'</li>';
    echo '<li>'.$row["name_2"].'</li>';
    echo '<li>'.$row["name_3"].'</li>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测