微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery .on()方法加载事件

我们如何使用jquery .on()方法加载事件例如代码是: –
<a href="#" id="add">Add</a>
<div id="initial">
     <input type="text" class="abc" name="in">
</div>

和它的jquery

jQuery(document).ready(function() {
    var x=$('#initial').html();
    $('#add').click(function(){
        $('body').append(x);
    });
    $(document).on('load','.abc',function(){
        alert('started');
    });
});

解决方法

参见 http://api.jquery.com/on/

它说

In all browsers,the load,scroll,and error events (e.g.,on an <img>
element) do not bubble. In Internet Explorer 8 and lower,the paste
and reset events do not bubble. Such events are not supported for use
with delegation,but they can be used when the event handler is
directly attached to the element generating the event.

如果要添加新的输入框时要做某事,则可以在添加代码之后直接写入代码

$('#add').click(function(){
        $('body').append(x);
        // Your code can be here
    });

如果您想要在文档中的第一个输入框加载时执行相同的代码,那么您可以在两个地方编写一个函数调用它,即$(‘#add’)。点击和文档的就绪事件

原文地址:https://www.jb51.cc/jquery/182414.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐