简体   繁体   English

jQuery live()函数,用于大量使用ajax网站

[英]jQuery live() function for purpose of heavy ajax use of site

Ive been looking for a way to make sure all scripts are triggered accordingly as content is loaded in and out of the page via jQuery load. 我一直在寻找一种方法,以确保通过jQuery加载将内容加载到页面中和从页面中加载出来时,所有脚本都被相应地触发。

The best method so far is the live function but I cannot get it to trigger functions on load, only operate with elements loaded in. 到目前为止,最好的方法是使用实​​时函数,但是我无法让它在加载时触发函数,只能在加载了元素的情况下进行操作。

EG. 例如。

$(document).ready(function(){
    $('textarea').live('keyup',function(){
        $(this).innerHeight(20 + (16 * ($(this).prop('scrollHeight') / $(this).css('line-height').replace('px', '') - 1.25)));
    });
    $('textarea').trigger('keyup');
});

This will make the textarea perform correctly on the keyup function but will not trigger the initial $('textarea').keyup(); 这将使textarea在keyup函数上正确执行,但不会触发初始的$('textarea').keyup();

How would I do that / is there a better way of achieving what I am after.` 我将如何做/是否有实现我所追求的更好的方法。

I have got this far but my initial call does not work once the page has loaded: 我已经做到了这一点,但是一旦页面加载后,我的初始呼叫将无法正常工作:

$(document).ready(function(){
    $(document).on('keyup', 'textarea',function(){
        $(this).innerHeight(20 + (16 * ($(this).prop('scrollHeight') / $(this).css('line-height').replace('px', '') - 1.25)));
    });
    $('textarea').trigger('keyup');
});

First, you want to use trigger() to fire an event. 首先,您要使用trigger()事件。

Also, don't use live() because it's deprecated. 另外,不要使用live()因为它已被弃用。 Instead use on() . 而是使用on() The syntax slightly changes: 语法略有变化:

$(document).ready(function(){
    $(document).on('keyup', 'textarea', function(){
        $(this).innerHeight(20 + (16 * ($(this).prop('scrollHeight') / $(this).css('line-height').replace('px', '') - 1.25)));
    });
    $('textarea').trigger('keyup');
});

The selector in $(document).on('keyup', 'textarea', function) works as follows: $(document).on('keyup', 'textarea', function)工作方式如下:

  1. Look for elements in document that are matching textarea (this also works for elements that are added after page load) document中查找与textarea匹配的元素(这也适用于页面加载后添加的元素)
  2. If the keyup event is triggered, execute the callback method 如果触发了keyup事件,请执行回调方法

Therefore, you can write something more specific than document to decrease the computation time. 因此,您可以编写比document更具体的document以减少计算时间。 For example: 例如:

$('#myForm').on('keyup', 'textarea', function)

Alp's response works perfectly. 阿尔卑斯山的反应非常完美。 The handler fires when you use trigger() so I don't understand where you're getting your error. 当您使用trigger()时,处理程序将trigger()因此我不知道您在哪里遇到错误。

Be sure that you trigger the keyup on your textarea AFTER you have loaded whatever dynamic content you are populating it with. 在加载任何动态内容 ,请确保在textarea上触发键入。 If you are using jQuery's load() to bring in content, trigger the keyup in the callback function. 如果您使用jQuery的load()引入内容,请在回调函数中触发keyup。

JS Fiddle JS小提琴

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

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