Ajax读取txt并对txt内容进行分页显示功能

时间:2021-05-26

下文给大家分享了ajax读取txt并对txt内容进行分页显示的核心代码,废话不多说了,直接贴代码了。

function TransferString(content){var string = content;try{string=string.replace(/\r\n/g,"<BR>")string=string.replace(/\n/g,"<BR>");string=string.replace(/[ ]/g,"&nbsp;")string=string.replace(/\ +/g,"&nbsp;")}catch(e) {alert(e.message);}return string;}var pageIndex = 1;var id = $("#aId").val();var url = $('#urlArticleContent').val();var txt = '';var pageSize = @(ConfigurationManager.AppSettings["ArticlepageSize"]);var page=5;var pageCount = 0;$(function() {if(url!=""){txt = ($.ajax({ url: url, async: false })).responseText;if (txt.indexOf('http://** @调用方法* $(selector).pagination();* * @更新日志* 2016-07-25:修复click重复事件*/; (function ($, window, document, undefined) {//配置参数var defaults = {totalData: 0, //数据总条数showData: 0, //每页显示的条数pageCount: 9, //总页数,默认为9current: 1, //当前第几页prevCls: 'prev', //上一页classnextCls: 'next', //下一页classprevContent: '上一页', //上一页内容nextContent: '下一页', //下一页内容activeCls: 'active', //当前页选中状态coping: false, //首页和尾页homePage: '', //首页节点内容endPage: '', //尾页节点内容count: 5, //当前页前后分页个数jump: true, //跳转到指定页数jumpIptCls: 'jump-ipt', //文本框内容jumpBtnCls: 'jump-btn', //跳转按钮jumpBtn: '跳转', //跳转按钮文本callback: function () { } //回调};var Pagination = function (element, options) {//全局变量var opts = options,//配置current,//当前页$document = $(document),$obj = $(element);//容器/*** 设置总页数* @param int page 页码* @return opts.pageCount 总页数配置*/this.setTotalPage = function (page) {return opts.pageCount = page;};/*** 获取总页数* @return int p 总页数*/this.getTotalPage = function () {var p = opts.totalData || opts.showData ? Math.ceil(parseInt(opts.totalData) / opts.showData) : opts.pageCount;return p;};//获取当前页this.getCurrent = function () {return current;};/*** 填充数据* @param int index 页码*/this.filling = function (index) {var html = '';current = index || opts.current;//当前页码var pageCount = this.getTotalPage();if (current > 1) {//上一页html += '<li onclick=GoPage(' + (current - 1) + ')><a href="javascript:;" class="' + opts.prevCls + '">' + opts.prevContent + '</a></li>';} else {$obj.find('.' + opts.prevCls) && $obj.find('.' + opts.prevCls).remove();}if (current >= opts.count * 2 && current != 1 && pageCount != opts.count) {var home = opts.coping && opts.homePage ? opts.homePage : '1';html += opts.coping ? '<li onclick=GoPage(' + home + ')><a href="javascript:;" data-page="1">' + home + '</a></li><span>...</span>' : '';}var start = current - opts.count,end = current + opts.count;((start > 1 && current < opts.count) || current == 1) && end++;(current > pageCount - opts.count && current >= pageCount) && start++;for (; start <= end; start++) {if (start <= pageCount && start >= 1) {if (start != current) {html += '<li onclick=GoPage(' + start + ')><a href="javascript:;" data-page="' + start + '">' + start + '</a></li>';} else {html += '<li class="' + opts.activeCls + '" onclick=GoPage(' + start + ')><a href="javascript:;"><span>' + start + '</span></a></li>';}}}if (current + opts.count < pageCount && current >= 1 && pageCount > opts.count) {var end = opts.coping && opts.endPage ? opts.endPage : pageCount;html += opts.coping ? '<span>...</span><li onclick=GoPage(' + pageCount + ')><a href="javascript:;" data-page="' + pageCount + '">' + end + '</a></li>' : '';}if (current < pageCount) {//下一页html += '<li onclick=GoPage(' + (current + 1) + ')><a href="javascript:;" class="' + opts.nextCls + '">' + opts.nextContent + '</a></li>'} else {$obj.find('.' + opts.nextCls) && $obj.find('.' + opts.nextCls).remove();}html += opts.jump ? '<input type="text" class="' + opts.jumpIptCls + '"><a href="javascript:;" onclick=PageGo() class="' + opts.jumpBtnCls + '">' + opts.jumpBtn + '</a>' : '';$obj.empty().html(html);};//绑定事件this.eventBind = function () {var self = this;var pageCount = this.getTotalPage();//总页数$obj.off().on('click', 'a', function () {if ($(this).hasClass(opts.nextCls)) {var index = parseInt($obj.find('.' + opts.activeCls).text()) + 1;} else if ($(this).hasClass(opts.prevCls)) {var index = parseInt($obj.find('.' + opts.activeCls).text()) - 1;} else if ($(this).hasClass(opts.jumpBtnCls)) {if ($obj.find('.' + opts.jumpIptCls).val() !== '') {var index = parseInt($obj.find('.' + opts.jumpIptCls).val());} else {return;}} else {var index = parseInt($(this).data('page'));}self.filling(index);typeof opts.callback === 'function' && opts.callback(self);});//输入跳转的页码$obj.on('input propertychange', '.' + opts.jumpIptCls, function () {var $this = $(this);var val = $this.val();var reg = /[^\d]/g;if (reg.test(val)) {$this.val(val.replace(reg, ''));}(parseInt(val) > pageCount) && $this.val(pageCount);if (parseInt(val) === 0) {//最小值为1$this.val(1);}});//回车跳转指定页码$document.keydown(function (e) {var self = this;if (e.keyCode == 13 && $obj.find('.' + opts.jumpIptCls).val()) {var index = parseInt($obj.find('.' + opts.jumpIptCls).val());GoPage(index);//self.filling(index);//typeof opts.callback === 'function' && opts.callback(self);}});};//初始化this.init = function () {this.filling(opts.current);this.eventBind();};this.init();};$.fn.pagination = function (parameter, callback) {if (typeof parameter == 'function') {//重载callback = parameter;parameter = {};} else {parameter = parameter || {};callback = callback || function () { };}var options = $.extend({}, defaults, parameter);return this.each(function () {var pagination = new Pagination(this, options);callback(pagination);});};})(jQuery, window, document);

以上所述是小编给大家介绍的Ajax读取txt并对txt内容进行分页显示,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章