我有两个用于分页和截断的javascript函数,如下所示:
<script type="text/javascript" src="//path/js/jquery.quick.pagination.min.js"></script>
$("#post_list").quickPagination({
pagerLocation:"both",
pageSize:"3"
});
<script type="text/javascript" language="javascript" src="//path/js/jquery.dotdotdot.js"></script>
$(".post_detail").dotdotdot({
after: 'a.more',
callback : function( isTruncated, orgContent ) {
if (!isTruncated) {
$('.more',this).remove()
}
},
});
每个都在onLoad上运行良好。但是,一旦触发了分页功能,truncate函数就不会再次运行。
请参阅我的jsfiddle并点击第2页以查看问题。
jsfiddle:http://jsfiddle.net/wr2ezv4f/1/
编辑:
我设法通过将truncate函数直接添加到jquery.quick.pagination.min.js文件中来包含truncate函数(请参阅js中的注释作为folow(AND工作正常):
(function($) {
"use strict";
$.fn.quickPagination = function(options) {
var defaults = {
pageSize: 10,
currentPage: 1,
holder: null,
pagerLocation: "after",
transitionSpeed: null
};
options = $.extend(defaults, options);
return this.each(function() {
var selector = $(this);
var pageCounter = 1;
if ($(".simplePagerContainer").length < 1) selector.wrap("<div class='simplePagerContainer'></div>");
selector.parents(".simplePagerContainer").find("ul.simplePagerNav").remove();
selector.children().removeClass(function(index, css) {
return (css.match(/simplePagerPage([0-9]+)?/ig) || []).join(' ');
}).filter(":visible").each(function(i) {
if (i < pageCounter * options.pageSize && i >= (pageCounter - 1) * options.pageSize) {
$(this).addClass("simplePagerPage" + pageCounter);
} else {
$(this).addClass("simplePagerPage" + (pageCounter + 1));
pageCounter++;
}
});
selector.children().hide();
selector.children(".simplePagerPage" + options.currentPage).show();
if (pageCounter <= 1) {
return;
}
var pageNav = "<ul class='simplePagerNav'>";
for (var i = 1; i <= pageCounter; i++) {
if (i == options.currentPage) {
pageNav += "<li class='page currentPage simplePageNav" + i + "'><a rel='" + i + "' href='#'>" + i + "</a></li>";
} else {
pageNav += "<li class='page simplePageNav" + i + "'><a rel='" + i + "' href='#'>" + i + "</a></li>";
}
}
pageNav += "</ul>";
if (!options.holder) {
switch (options.pagerLocation) {
case "before":
selector.before(pageNav);
break;
case "both":
selector.before(pageNav);
selector.after(pageNav);
break;
default:
selector.after(pageNav);
}
} else {
$(options.holder).append(pageNav);
}
selector.parent().find(".simplePagerNav a").click(function(e) {
e.preventDefault();
var clickedLink = $(this).attr("rel");
options.currentPage = clickedLink;
var container = options.holder ? options.holder : ".simplePagerContainer";
$(this).parents(container).find("li.currentPage").removeClass("currentPage");
$(this).parents(container).find("a[rel='" + clickedLink + "']").parent("li").addClass("currentPage");
selector.children().hide();
if (options.transitionSpeed) {
selector.find(".simplePagerPage" + clickedLink).fadeIn(options.transitionSpeed);
// ** I managed to include the truncate function here **//
$(".post_title").dotdotdot();
$(".post_detail").dotdotdot({
after: 'a.more',
callback : function( isTruncated, orgContent ) {
if (!isTruncated) {
$('.more',this).remove()
}
},
});
} else {
selector.find(".simplePagerPage" + clickedLink).show();
//** and to include the truncate function here **//
$(".post_title").dotdotdot();
$(".post_detail").dotdotdot({
after: 'a.more',
callback : function( isTruncated, orgContent ) {
if (!isTruncated) {
$('.more',this).remove()
}
},
});
}
return false;
});
});
};
})(jQuery);
我现在的问题是,如何修改此js文件,以便我可以在js文件之外包含此函数:
类似的东西:
$("#post_list").quickPagination({
pagerLocation:"both",
pageSize:"3"
// ** optional function I want to include here **/
});
答案 0 :(得分:0)
您可以在这样的常用功能中编写这两个功能
$(function()
{
$("#post_list").quickPagination({
pagerLocation:"both",
pageSize:"3"
});
$(".post_title").dotdotdot();
$(".post_detail").dotdotdot({
after: 'a.more',
callback : function( isTruncated, orgContent ) {
if (!isTruncated) {
$('.more',this).remove()
}
},
});
});