//删除左边的空格 function ltrim(str){ return str.replace(/(^\s*)/g,""); } //补零函数 function check(num){ var number = num < 10 ? "0" + num : num; return number; } //获取日期 function cTimeDate(now){ var now = now * 1000; var newTime = new Date(now); var month = newTime.getMonth()+1; var date = newTime.getDate(); var html = month+"月"+date+"日"; return html; } //获取时间 function cTime(now){ var now = now * 1000; var newTime = new Date(now); var hours = newTime.getHours(); var minutes = newTime.getMinutes(); var html = check(hours)+":"+check(minutes); return html; } //多张图片时,获取第一张图片 function getPicPath(pic){ var picPath = pic?pic.split("\n")[0]:""; return picPath; } //获取新闻链接 这样提取 为了规避发布后台/683/2013/0307/edit.js被解析的问题 function getnewsUrl(url) { var newsurl=url; return newsurl; } //显示新闻 function showNews( data ) { var data=data.data; var html = ''; var _id = ''; var _code = ''; var _class = ''; //得到个数 if(data.length==0){ $( "#articleList" ).hide(); } else { var title_bread = $('.label_name').eq(1).html();; for(var i in data){ //console.log(data[i]); var _date = data[i].createtime; var _time = _date.split(/\s+/); _id = data[i].zw_id; _id = _id == 0 ? data[i].HDid : _id; var high = new RegExp("http:\/\/photo/"); var video = new RegExp("video"); var um = new RegExp("http:\/\/um\.auto"); var _pic = new RegExp("2014\/0331\/20140331093614\.jpg"); if (high.test(data[i].url)) { _code = 'highpix_'+ _id; } else if (video.test(data[i].url)) { _code = 'video_'+ _id; } else if (um.test(data[i].url)) { _code = 'um_'+ _id; } else { _code = 'news_'+ _id; } var label = data[i]['label_name'] != '' && data[i]['label_name'] != title_bread ? '标签:'+ data[i]['label_name'] +'' : ''; //(一)文字 if(_pic.test(data[i].pic)){ //console.log("no-picture"); }else{//(二)图文 //console.log("has-picture"); var _title = data[i].shorttitle ==''? data[i].title : data[i].shorttitle; html += '
  • ' + _title +
 	'
    ' + _title + '

    ' + ltrim(data[i].abstract) + '

    ' + _date + '
  • '; } $( "#articleList" ).html(html); } } } //报错信息 function getdataerror(){ $( "#articleList").html("

    获取数据失败

    "); } //添加内容 function init_html(page){ var cur_dataurl="http://news.auto.sina.com.cn/m/label/get_label_info.php?label="+ encodeURIComponent(window.lmName) +"&length=" + prPageNum + "&page=" + page; //console.log("value1:",cur_dataurl); $.ajax({ dataType: "jsonp", url: cur_dataurl, success: showNews, error: getdataerror }); } //判断是否有手动添加数据 //function if_shoudong(){ // var shoudong = encodeURIComponent(window.shoudong); // console.log("-shoudong"); // if(shoudong){ // console.log("has-shoudong"); // console.log(shoudong); // } //} init_html(1); //if_shoudong(); //ajax var initNEWSpage; var cur_dataurl = "http://news.auto.sina.com.cn/m/label/get_label_info.php?label=" + encodeURIComponent(window.lmName) + "&callback=?&length="+ prPageNum; $.ajax({ dataType: "jsonp", url: cur_dataurl, success: function(data) { $(".pub .num").html(data.total); initNEWSpage = $.navCount($("#pageLabels"), "pageLabel", ""); initNEWSpage.setTotal(data.total); var curpage = 1; var index = location.href.indexOf("?newspage="), base; if (index >= 0) { curpage = location.href.split("?newspage=")[1]; } else { curpage = 1; } initNEWSpage.setCurrent(curpage); }, error: getdataerror }); //分页算法 jQuery.extend({ navCount: function(ul, className, pagename) { this.el = ul; this.itemClass = className; this.total = 1; this.prePageNUM = prPageNum; this.currentPage = 1; this.pname = pagename; this.setTotal = function(count) { count = Number(count); this.toggleNav(count); count = Math.ceil(count / this.prePageNUM); this.total = count; $.each(this.el.children(".pageNum" + this.pname), function(index, item) { $(item).text(index + 1) }); this.el.children("a.total" + this.pname).text(count); this.el.children(".pageLabel" + this.pname).show(); if (count <= 6) { this.el.children(".pageNum" + this.pname + ":gt(" + (count - 1).toString() + ")").hide(); } else if (count == 7) { this.el.children(".text" + this.pname).hide(); } }; this.setCurrent = function(index) { this.currentPage = index; var t = this.total; this.el.children(".pageLabel").removeClass("current"); if (index <= 5) { this.el.children(".text:first").hide(); if (t <= 6) { this.el.children(".text:last").hide(); } $(this.el.children(".pageNum")[index - 1]).addClass("current"); $.each(this.el.children(".pageNum:not(.first,.total)"), function(i, item) { $(item).text(2 + i); }); } else if (index >= 6 && index <= t - 4) { this.el.children(".text:first").show(); this.el.children(".text:last").show(); $(this.el.children(".pageNum")[3]).addClass("current"); $.each(this.el.children(".pageNum:not(.first,.total)"), function(i, item) { $(item).text(index - 2 + i); }); } else { this.el.children(".text:last").hide(); $.each(this.el.children(".pageNum:not(.first,.total)"), function(i, item) { $(item).text(t - 5 + i); }); if (index == t) { if (index == 6) { $.each(this.el.children(".pageNum:not(.first,.total)"), function(i, item) { $(item).text(t - 4 + i); }); this.el.children(".pageNum:not(.total):last").addClass("current"); } else { this.el.children(".pageNum:last").addClass("current"); } } else { $(this.el.children(".pageNum:not(.first,.total)")[5 - t + Number(index)]).addClass("current"); } } }; this.toggleNav = function(t) { if (t) { this.el.show(); } else { this.el.hide(); } }; this._init = function() { var nav = this; $(".pageNum").on("click", function(e) { var index = location.href.indexOf("?newspage="), base; if (index >= 0) { base = location.href.substr(0, index - 1); } else { base = location.href; } document.location.hash = "?newspage=" + $(this).text(); init_html($(this).text()); nav.setCurrent($(this).text()); window.scrollTo('#articleList',800); }); $(".prev").on("click", function(e) { if (nav.currentPage > 1) { var index = location.href.indexOf("?newspage="), base; if (index >= 0) { base = location.href.substr(0, index - 1); } else { base = location.href; } document.location.hash = "?newspage=" + (nav.currentPage - 1).toString(); init_html(nav.currentPage - 1); nav.setCurrent(nav.currentPage - 1); window.scrollTo('#articleList',800); } }); $(".next").on("click", function(e) { if (nav.currentPage < nav.total) { var index = location.href.indexOf("?newspage="), base; if (index >= 0) { base = location.href.substr(0, index - 1); } else { base = location.href; } document.location.hash = "?newspage=" + (Number(nav.currentPage) + 1).toString(); init_html(Number(nav.currentPage) + 1); nav.setCurrent(Number(nav.currentPage) + 1); //window.scrollTo(0,0);articleList window.scrollTo('#articleList',800); } }); return this; } return this._init(); } });