jquery .on()方法加载事件 [英] jquery .on() method with load event

查看:148
本文介绍了jquery .on()方法加载事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何使用带有load事件的jquery .on()方法,例如代码是: -

How can we use jquery .on() method with load event for eg the code is:-

<a href="#" id="add">Add</a>
<div id="initial">
     <input type="text" class="abc" name="in">
</div>



和它的jquery


And the jquery for it

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/

它说



It says


在所有浏览器中,加载,滚动和错误事件(例如,< img>
元素)不要泡。在Internet Explorer 8和更低版本中,粘贴
和重置事件不会浮动。这样的事件不支持使用
与委托,但是当事件处理程序是
直接附加到生成事件的元素时,可以使用这些事件。

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.

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

If you want to do something when a new input box is added then you can simply write the code after appending it.

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

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

And if you want the same code execute when the first input box within the document is loaded then you can write a function and call it in both places i.e. $('#add').click and document's ready event

这篇关于jquery .on()方法加载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆