XML內容:
<?xml version="1.0" encoding="utf-8"?><info><user><name>王小虎</name><sex>男</sex><habit>電腦遊戲</habit></user><user><name>張小凡</name><sex>男</sex><habit>體育遊戲</habit></user><user><name>卓不凡</name><sex>男</sex><habit>網球遊戲</habit></user></info>
html內容:
<!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>Untitled Document</title><script type="text/javascript" src="JS/JQ.js"></script><script type="text/javascript" src="JS/JQ008.js"></script></head><body><p>改變輸入框的內容(王、張、卓中任意)</p><input type="text" style="width:325px; height:25px;" /></div></body></html>
JS內容:
$(document).ready(function(e) { $(":text").change(function(){try{$.ajax({type:"GET",url:"xml.xml",success:function(xml){$(xml).find("user").each(function(i) { var name=$(this).children("name");var name_value=$(this).children("name").text();var sex_value=$(this).children("sex").text();var habit_value=$(this).children("habit").text();var text=$(":text").val();if($(this).children("name:contains('"+text+"')").length>0){alert("姓名:"+name_value+",性別:"+sex_value+",愛好:"+habit_value);} });}});}catch(e){alert(e);}});});
後面想寫一個類似baidu搜尋時 輸入一個字元 會出現後面字元的效果 後面會放在下面
仿baidu搜尋 輸入字元 出現提示字
XML部分:
<?xml version="1.0" encoding="utf-8"?><info><user><name>王小虎</name><sex>男</sex><habit>電腦遊戲</habit></user><user><name>王二虎</name><sex>男</sex><habit>電腦遊戲</habit></user><user><name>王三虎</name><sex>男</sex><habit>電腦遊戲</habit></user><user><name>王小丫</name><sex>男</sex><habit>電腦遊戲</habit></user><user><name>張小凡</name><sex>男</sex><habit>體育遊戲</habit></user><user><name>張二凡</name><sex>男</sex><habit>體育遊戲</habit></user><user><name>張三凡</name><sex>男</sex><habit>體育遊戲</habit></user><user><name>張無忌</name><sex>男</sex><habit>體育遊戲</habit></user><user><name>卓不凡</name><sex>男</sex><habit>網球遊戲</habit></user><user><name>卓一凡</name><sex>男</sex><habit>網球遊戲</habit></user><user><name>卓二凡</name><sex>男</sex><habit>網球遊戲</habit></user><user><name>卓天凡</name><sex>男</sex><habit>網球遊戲</habit></user></info>
HTML部分:
<!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>Untitled Document</title><script type="text/javascript" src="JS/JQ.js"></script><script type="text/javascript" src="JS/JQ008.js"></script></head><style>.small{ width:325px; height:14px; font-family:"微軟雅黑";float:left; margin:5px;}</style><body><input type="text" style="width:325px; height:25px;" /><div id="show" style="width:315px; border:1px solid #CCC; height:auto; position:absolute; top:40px; margin:0px; padding:0px;"><div class="small"></div></div></body></html>
JS部分:
$(document).ready(function(e) { $(":text").keyup(function(){try{$("#show").html("");$.ajax({type:"GET",url:"xml.xml",success:function(xml){$(xml).find("user").each(function(i) { var name=$(this).children("name");var name_value=$(this).children("name").text();var sex_value=$(this).children("sex").text();var habit_value=$(this).children("habit").text();var text=$(":text").val();if($(this).children("name:contains('"+text+"')").length>0){$("#show").append("<div class='small'>" +name_value+"</div>");} });$("#show").append("<div class='small'></div>");}});}catch(e){alert(e);}});});