<!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" />
<link rel="stylesheet" href="script/jquery.autocomplete.css" type="text/css">
<script src="script/j_1.6.1_utf.js" type="text/javascript"></script>
<script type="text/javascript" src="script/jquery.autocomplete.js"></script>
<script type="text/javascript">
jQuery().ready(function($){
/*================================test use json*/
$("#btnTest").click(function() {
sohuGetList2();
});
/*AutoComplete json*/
var sohuSearchUrl="http://api.tv.sohu.com/search.json?api_key=e5c8865ad74e7c26c874e599fd1fbdc1&callback=?";
$("#key").autocomplete(sohuSearchUrl, {
dataType : 'json', // 必须填写,不然默认的是text
autoFill : true, // 自动填充 可选
mustMatch: true,
extraParams : {
key: function (){
return $('#key').val();
}
},//支持像后台传递参数,key为你后台接收参数的名称
parse : function(data) { // 处理返回的json串,以供后续的使用
if(data.status == 200){
var videos = data.data.videos || [];
var rows = []; // 处理后 返回的一个 数组
for ( var i = 0; i < videos.length; i++) { // 如果你返回的是一个 类似{'my':[{'name':'value1'},{'name':'value2'}]}
rows[rows.length] = {
data : videos[i], //返回的参数,供后续的函数调用
value : videos[i].tv_name, //鼠标经过时 在 输入框显示的值
result : videos[i].tv_name //选中后在 输入框显示的值
}
}
}
return rows;
},
formatItem : function(data, i, total) { //代表的是显示的格式
return data.tv_name;
},
formatMatch : function(data, i, total) { //表示匹配的内容
return data;
},
formatResult : function(data) { //表示结果的内容
return data;
}
});
//选定点击事件
$("#key").result(function(event, data, formatted) { //event: 事件对象. event.type为result. //data: 选中的数据行. // formatted:formatResult函数返回的值
if(data != undefined){
var tip_num = data.tip_num;
var num = tip_num.substr(tip_num.indexOf('/')+1,tip_num.length-tip_num.indexOf('/'));
sohuGetList(data.sid,num);
}
});
//获取sohu影片具体列表
var sohuGetList= function(sid,num){
$('ol').html('');//clear
var reqUrl = "http://api.tv.sohu.com/set/list/"+sid+".json?api_key=e5c8865ad74e7c26c874e599fd1fbdc1&callback=?&pageSize=" + num;
$('#aid').html(reqUrl);
$.getJSON(reqUrl,function(data){
if(data.status == 200){
videos = data.data.videos || [];
//alert(videos.length);
for(var i=0;i<videos.length;++i){
var url="http://open.tv.sohu.com/play.do?api_key=e5c8865ad74e7c26c874e599fd1fbdc1&vid=" + videos[i].tv_ver_id;
var title = videos[i].tv_name;
$('<li></li>').html(title + ' | ' + url).appendTo('ol');
}//end for
}//end if
});//end getjson
};
var sohuGetList2= function(){
$('ol').html('');//clear
var url = sohuSearchUrl + "&key=" + $('#key').val();
$.getJSON(url,function(data){
if(data.status == 200){
videos = data.data.videos || [];
var tip_num = videos[0].tip_num;
var num = tip_num.substr(tip_num.indexOf('/')+1,tip_num.length-tip_num.indexOf('/'));
var sid = videos[0].sid;
sohuGetList(sid,num);
}//end if
});//end getjson
};
/*============================end test use json*/
/*------------------------------search use xml*/
$("#btnSearch").click(function() {
$('ol').html('');//clear
var istv = $("#istv").attr("checked");
var kw = $("#title").val();
$.ajax({
url:"http://api.tv.sohu.com/search.xml?api_key=e5c8865ad74e7c26c874e599fd1fbdc1&key="+encodeURIComponent(kw),
type: 'GET',
dataType: 'xml',
timeout: 10000,
error: function(xml){
//alert('Error loading XML document'+xml);
},
success: function(xml){
$(xml).find("item").each(function(i){
if(i==0)
{
var id=$(this).children("sid").text();
var tip_num = $(this).children("tip_num").text();
var num = tip_num.substr(tip_num.indexOf('/')+1,tip_num.length-tip_num.indexOf('/'));
num = num +20;
$("#aid").html(id + " | " + encodeURIComponent(kw) + " | " + num);
/*根据电视id获取*/
$.ajax({
url:"http://api.tv.sohu.com/set/list/"+id+".xml?api_key=e5c8865ad74e7c26c874e599fd1fbdc1&pageSize=" + num,
type: 'GET',
dataType: 'xml',
timeout: 10000,
error: function(xml){
alert('Error loading XML document'+xml);
},
success: function(xml){
$(xml).find("item").each(function(i){
var id=$(this).children("tv_ver_id").text();
var title=$(this).children("tv_name").text();
$('<li></li>').html(id + ' | ' + title).appendTo('ol');
});
}
});//end ajax
}
}); //end each
}//end success
});
});
/*AutoComplete*/
$("#title").autocomplete("data.asp", {
width: 320,
max: 40,
highlight: false,
width:200,
scroll: true,
scrollHeight: 300,
mustMatch :true,
formatItem: function(data, i, n, value) {
return value;
},
formatResult: function(data, value) {
return value.split(".")[0];
}
});
});
//选定点击事件
// $("#title").result(function(event, data, formatted) {
// if(data != undefined)
// alert(data);
// });
/*-------------------------------------end search use xml*/
</script>
<style type="text/css">
ol{ float:left}
#title{ width:200px}
#sdiv{ width:100%; margin-top:20px;}
</style>
<title>SOHU</title>
</head>
<body>
<input type="text" id="title" /><input type="button" id="btnSearch" value="search xml" />
<input name="key" id="key" type="text" class="tx" />
<input type="button" class="btn" id="btnTest" value="search json" />
<label></label>
<div id="aid"></div>
<div id="sdiv"><ol></ol></div>
</body>
</html>