简体   繁体   English

jQuery插件无法在Ajax加载的页面上运行

[英]jQuery plugin not working on ajax-loaded page

I am using jquery prettyPopin in one of my projects and it is working fine ... Problem is i want it to work on the php page loaded using ajax. 我在我的项目之一中使用jquery prettyPopin,它工作正常……问题是我希望它在使用ajax加载的php页面上工作。 But it does not seem to work ... 但这似乎不起作用...

My question in short is whether a jQuery plugin works in a ajax-loaded page as it does on the parent page? 简而言之,我的问题是,jQuery插件是否可以像在父页面上一样在ajax加载的页面中工作? To make it clear my JS libraries are added in the parent page. 为了清楚起见,我的JS库已添加到父页面中。 No libraries are added in the loading script. 加载脚本中未添加任何库。

Do the jQuery plugins work on the ajax pages ? jQuery插件可以在ajax页面上使用吗? Also, i do not want to include js libraries in ajax page. 另外,我不想在ajax页面中包含js库。

Any help is welcome ... Thanx in advance ... 欢迎任何帮助...提前感谢...

any jquery plugin will work in ajax loaded pages as well. 任何jquery插件也将在ajax加载的页面中工作。 but if you are trying to call it in ajax loaded page, you need to call plugin after ajax completed. 但是,如果您尝试在ajax加载页面中调用它,则需要在ajax完成后调用插件。

Yes it does work, but you have to initialize any plugins after the page has been loaded. 是的,它确实可以工作,但是您必须在页面加载后初始化所有插件。 Example when using .load() : 使用.load()的示例:

$("#page").load("/page.php", function(response, status, xhr) {
    $('#selector-on-loaded-page').plugin();
});

You just have to include the plugin as usual on the main page. 您只需要照常在主页上包含该插件即可。

There may be a jQuery conflict you can use your code like this 可能存在jQuery冲突,您可以像这样使用您的代码

var $jq = jQuery.NoConflict();
$jq("#page").load("/page.php", function(response, status, xhr) {
    $jq('#selector-on-loaded-page').plugin();
});

It should work because the included js can be used on the loaded DOM and the included js. 它应该工作,因为包含的js可以在加载的DOM和包含的js上使用。

Yet, it depends on how the plugin works and how you are calling it. 但是,这取决于插件的工作方式以及您如何调用它。 For instance, it will likely not work if it gets dynamically applied onload or if you tried to wrap it inside your $(document).ready() of your main code. 例如,它可能不会得到,如果动态地应用它工作onload或者如果你试图把它包你里面$(document).ready()的主代码。

[EDIT] According to the doc of the plugin: [编辑]根据插件文档

Don't forget to apply the plugin again for instance, you have to put: 例如,不要忘记再次应用该插件,您必须输入:

$("a[rel^='prettyPopin']").prettyPopin();

in the callback of your .load() statement. .load()语句的回调中。

So lets say you are using the plugin "foo" this way : 因此,假设您正在以这种方式使用插件“ foo”:

$(".something").foo();

in the end of the ajax request (once you get the data back from php and create the new elements) re-write the same line $(".something").foo(); 在ajax请求的末尾(一旦您从php获取数据并创建新元素),重新编写同一行$(“。something”)。foo();

another way would be to figure out how to integrate live in the way you use the plugin 另一种方法是弄清楚如何以使用插件的方式实时集成

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

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