jquery移动与现场活动

时间:2015-02-12 11:33:20

标签: jquery jquery-mobile

我希望有人会帮助我。

我想使用下面的代码片段将元素插入到dom中添加keydown或任何其他事件等事件。我正在从服务器加载html。

$(document).delegate("#services", "pagebeforecreate", function(inEvent) {
     $.ajax({
         url:appUrl+"/tvserver/index.php/smartbox/services",
         dataType:"html",
         timeout:5000,
         success:function(data){
             $('#serviceContent').html(data);
         }
     });

});

这是ajax到页面的输出,我想将事件添加到

li element

<div class="services-body">
    <ul id="services">
                            <li data-name="Lanudary" data-description="laudary services for customers" data-url="http://localhost/tvserver/uploads/default.png" data-id="1" data-payable="Y" data-cost="2.00">
                    <div>
                        <img src="http://localhost/tvserver/uploads/default">
                    </div>
                    <div class="services-names">
                       <span class="service-name">Lanudary</span>
                    </div>
                </li>
                            <li data-name="Gym" data-description="a place to train " data-url="http://localhost/tvserver/uploads/default.png" data-id="2" data-payable="N" data-cost="0.00">
                    <div>
                        <img src="http://localhost/tvserver/uploads/default">
                    </div>
                    <div class="services-names">
                       <span class="service-name">Gym</span>
                    </div>
                </li>
                <li data-name="Spa" data-description="Massaging of the body" data-url="http://localhost/tvserver/uploads/default" data-id="3" data-payable="Y" data-cost="5.00">
                    <div>
                        <img src="http://localhost/tvserver/uploads/default.png">
                    </div>
                    <div class="services-names">
                       <span class="service-name">Spa</span>
                    </div>
                </li>
                </ul>
</div>

感谢

1 个答案:

答案 0 :(得分:1)

这是一种工作方式

function eventClick(obj){
        $(obj).on("click",function(){
            // some function
        });
    }
    $.ajax({
          url:appUrl+"/tvserver/index.php/smartbox/services",
          dataType:"html",
          timeout:5000,
          success:function(data){
                var d=$(data);
                d.find("#services li").each(function(){
                    eventClick(this);//add event lisener
                });
               $('#serviceContent').html(d);
            }
    });