在jquery加载内容后执行函数。

时间:2021-06-29 00:00:45

I'm dynamically inserting a chunk of HTML that is the content of a carousel using jQuery load...

我动态地插入了一大块HTML,这是使用jQuery加载的旋转木马的内容……

$('button').on('click', function() {
    $('#endpoint').load('content.html #test);
    $('#endpoint').css('display','block');
    $('#endpoint #slides').owlCarousel();
});

The carousel won't initialise and I can't work out why! If I run the carousel init into the console once the DOM is amended then it works fine.

旋转木马不会启动,我也不知道为什么!如果我在修改DOM之后将旋转木马init运行到控制台,那么它就可以正常工作了。

I've tried sticking the carousel init in a timeout as well with no luck.

我也尝试过把旋转木马initin插入超时,但运气不佳。

Do I need to look at a full ajax call so I can run it from a callback? Any advice appreciated!

我是否需要查看完整的ajax调用,以便从回调运行它?任何建议感激!

4 个解决方案

#1


1  

You need to use callback method of .load() which executes when the request completes.

您需要使用.load()的回调方法,该方法在请求完成时执行。

$('button').on('click', function() {
    $('#endpoint').load('content.html #test' function(){ //Also missing closing quotes
        $('#endpoint').css('display','block');
        $('#endpoint #slides').owlCarousel();
    });     
});

#2


2  

Because there's a syntax error ?

因为有语法错误?

$('#endpoint').load('content.html #test);  <- quote missing after #test

#3


0  

Run the function when the document is ready:

当文件准备好时运行函数:

$(document).ready( function () {
$('button').on('click', function() {
    $('#endpoint').load('content.html #test');
    $('#endpoint').css('display','block');
    $('#endpoint #slides').owlCarousel();
});
});

#4


0  

A single quote is missing at the end of this : Just replace this :

一段引语在结尾时缺失了:

$('#endpoint').load('content.html #test);

By this (just add the single quote at the end of #test) :

通过这个(在测试结束时添加单引号):

$('#endpoint').load('content.html #test');

#1


1  

You need to use callback method of .load() which executes when the request completes.

您需要使用.load()的回调方法,该方法在请求完成时执行。

$('button').on('click', function() {
    $('#endpoint').load('content.html #test' function(){ //Also missing closing quotes
        $('#endpoint').css('display','block');
        $('#endpoint #slides').owlCarousel();
    });     
});

#2


2  

Because there's a syntax error ?

因为有语法错误?

$('#endpoint').load('content.html #test);  <- quote missing after #test

#3


0  

Run the function when the document is ready:

当文件准备好时运行函数:

$(document).ready( function () {
$('button').on('click', function() {
    $('#endpoint').load('content.html #test');
    $('#endpoint').css('display','block');
    $('#endpoint #slides').owlCarousel();
});
});

#4


0  

A single quote is missing at the end of this : Just replace this :

一段引语在结尾时缺失了:

$('#endpoint').load('content.html #test);

By this (just add the single quote at the end of #test) :

通过这个(在测试结束时添加单引号):

$('#endpoint').load('content.html #test');