基于JQuery的抓取博客園首頁RSS的代碼
更新時(shí)間:2011年12月01日 23:00:20 作者:
JQuery 抓取博客園首頁RSS直接將代碼保存到文本文檔,更改后綴名為html瀏覽 即可查看效果
效果圖:

實(shí)現(xiàn)代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
</head>
<body>
<table id='tbl1' cellpadding="1" cellspacing="1" bgcolor="#333333" width="800px" style="line-height:30px;">
<tr bgcolor="#FFFFFF"><td align="center" width="70%">標(biāo)題</td><td align="center" width="30%">時(shí)間</td></tr>
</table>
<div id="loading" style="display:none"><font color='red'>正在加載數(shù)據(jù)。。。</font></div>
</body>
</html>
<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script language="javascript" type="text/javascript" >
$(function(){
var html="";
var bgcolor="";
$.ajax({
url:"http://www.cnblogs.com/rss",
type:"get",
//dataType:($.browser.msie) ? "text" : "xml",
success:function(data){
$("item",data).each(function(index,element){
bgcolor=index%2==0 ?" bgcolor='#F1F1F1' ":" bgcolor='#FFFFFF' ";
html+="<tr "+bgcolor+"><td><a href='"+$(this).find("link").text()+"'>"+FormatContent($(this).find("title").text(),40)+"</td><td>"+ new Date($(this).find("pubDate").text()).format("yyyy-MM-dd hh:mm:ss");+"</td></tr>";
});
$("#tbl1 tr:not(':first')").remove();//移除非第一行
$("#tbl1").append(html);//綁定數(shù)據(jù)到table
},
complete:function(){
$("#loading").hide();
},
beforeSend:function(x){
//x.setRequestHeader("Content-Type", "charset=utf-8");
$("#loading").show();
},
error:function(){
alert("error");
}
});
});
</script>
<script language="javascript">
/**
* 時(shí)間對象的格式化;
*/
Date.prototype.format = function(format) {
/*
* eg:format="YYYY-MM-dd hh:mm:ss";
*/
var o = {
"M+" :this.getMonth() + 1, // month
"d+" :this.getDate(), // day
"h+" :this.getHours(), // hour
"m+" :this.getMinutes(), // minute
"s+" :this.getSeconds(), // second
"q+" :Math.floor((this.getMonth() + 3) / 3), // quarter
"S" :this.getMilliseconds()
// millisecond
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "")
.substr(4 - RegExp.$1.length));
}
for ( var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
: ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
//格式化標(biāo)題信息
function FormatContent(word,length){
return word.length>length?word.substring(0,length)+"...":word;
}
</script>

實(shí)現(xiàn)代碼:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
</head>
<body>
<table id='tbl1' cellpadding="1" cellspacing="1" bgcolor="#333333" width="800px" style="line-height:30px;">
<tr bgcolor="#FFFFFF"><td align="center" width="70%">標(biāo)題</td><td align="center" width="30%">時(shí)間</td></tr>
</table>
<div id="loading" style="display:none"><font color='red'>正在加載數(shù)據(jù)。。。</font></div>
</body>
</html>
<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script language="javascript" type="text/javascript" >
$(function(){
var html="";
var bgcolor="";
$.ajax({
url:"http://www.cnblogs.com/rss",
type:"get",
//dataType:($.browser.msie) ? "text" : "xml",
success:function(data){
$("item",data).each(function(index,element){
bgcolor=index%2==0 ?" bgcolor='#F1F1F1' ":" bgcolor='#FFFFFF' ";
html+="<tr "+bgcolor+"><td><a href='"+$(this).find("link").text()+"'>"+FormatContent($(this).find("title").text(),40)+"</td><td>"+ new Date($(this).find("pubDate").text()).format("yyyy-MM-dd hh:mm:ss");+"</td></tr>";
});
$("#tbl1 tr:not(':first')").remove();//移除非第一行
$("#tbl1").append(html);//綁定數(shù)據(jù)到table
},
complete:function(){
$("#loading").hide();
},
beforeSend:function(x){
//x.setRequestHeader("Content-Type", "charset=utf-8");
$("#loading").show();
},
error:function(){
alert("error");
}
});
});
</script>
<script language="javascript">
/**
* 時(shí)間對象的格式化;
*/
Date.prototype.format = function(format) {
/*
* eg:format="YYYY-MM-dd hh:mm:ss";
*/
var o = {
"M+" :this.getMonth() + 1, // month
"d+" :this.getDate(), // day
"h+" :this.getHours(), // hour
"m+" :this.getMinutes(), // minute
"s+" :this.getSeconds(), // second
"q+" :Math.floor((this.getMonth() + 3) / 3), // quarter
"S" :this.getMilliseconds()
// millisecond
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "")
.substr(4 - RegExp.$1.length));
}
for ( var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
: ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
//格式化標(biāo)題信息
function FormatContent(word,length){
return word.length>length?word.substring(0,length)+"...":word;
}
</script>
相關(guān)文章
jquery對象和javascript對象即DOM對象相互轉(zhuǎn)換
對于已經(jīng)是一個(gè) DOM 對象,只需要用 $() 把DOM對象包裝起來,就可以獲得一個(gè) jQuery 對象了,使用[index]和.get(index)可以轉(zhuǎn)為DOM對象2014-08-08
jQuery實(shí)現(xiàn)調(diào)節(jié)字體大小案例
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)調(diào)節(jié)字體大小案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Jquery 綁定時(shí)間實(shí)現(xiàn)代碼
jquery 顯示本地時(shí)間/ 帶日期、星期/ UTC,需要的朋友可以參考下。2011-05-05
innerHTML與jquery里的html()區(qū)別介紹
我原本一直以為innerHTML和jquery里的html其實(shí)是完全一樣的,jquery是多此一舉了,直到我遇到一次問題2012-10-10
jQuery實(shí)現(xiàn)動態(tài)生成年月日級聯(lián)下拉列表示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)動態(tài)生成年月日級聯(lián)下拉列表,結(jié)合具體實(shí)例形式分析了jQuery日期時(shí)間運(yùn)算構(gòu)造級聯(lián)下拉列表相關(guān)操作技巧,需要的朋友可以參考下2019-05-05

