[英]Load jquery plugin js after ajax page load content
Foe Example, I have page that use select2
plugin: 敌人的例子,我有使用select2
插件的页面:
function Initialize()
{
if($('.su-tech').length) {
$('.su-tech').select2({
allowClear: true,
width: '100%'
});
}
}
and html code like this: 和这样的html代码:
<select class="select2 su-tech" style="width:100%;">
<option value="1" >option1</option>
<option value="2" >option2</option>
<option value="3" >option3</option>
</select>
also I use ajax to load html content with jQuery: 我也用ajax用jQuery加载html内容:
$(document).on("click", "#page1", function () {
$.ajax({
type: 'POST',
url: "/example-page1",
success: function (result) {
$('.ajax_content').html(result);
Initialize()
},
})
})
Now I want to load js
and css
each jquery plugin dynamically: 现在我想动态加载js
和css
每个jquery插件:
<link rel="stylesheet" href="assets/vendors/select2/css/select2.min.css">
<script type="text/javascript" src="assets/vendors/select2/js/select2.min.js"></script>
Try this 尝试这个
//Declare a script with id
<script type="text/javascript" id="pluginJS"></script>
$(document).ready(function () {
$("#pluginJS").attr("src", "pluginJS.js");
$("#pluginJS").load(function () {
$("#otherJS").attr("src", "other-plugin.js");
});
})
Call after Ajax Success Ajax成功后致电
$(document).on("click", "#page1", function () {
$.ajax({
type: 'POST',
url: "/example-page1",
success: function (result) {
$('.ajax_content').html(result);
//you can call JS Here
$("#pluginJS").attr("src", "pluginJS.js");
$("#pluginJS").load(function () {
//After Load PluginJS you can use jQuery
Initialize();
});
},
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.