Posts Tagged ‘jQuery’

jQuery 1.3.2 Selectors Demo

9 三月, 2009

Selectors:Hierarchy

  1. ancestor descendant
    選出所有ancestor之下的descendant元素,不論descendant位於ancestor之下幾層。
    類似xpath語法://ancestor//descendant
  2. parent > child
    選出parent的子元素。
    類似xpath語法://parent/child
  3. prev + next
    選出prev元素接著next元素的所有next元素。
    類似的xpath語法://prev/next
  4. prev ~ siblings
    選出prev的所有siblings
    類似的xpath語法://prev/siblings/following-sibling::*
<!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>jQuery 測試</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" mce_src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<body>
<div id="msg_div">default text</div>
<form name="f1">
<span>
<input type="text" name="text1" value="this is f1 text1 value" /><br />
<input type="text" name="text2" value="this is f1 text2 value" /><br />
</span>
<input type="button" value="Demo parent child" id="btn1" /><br />
<input type="button" value="Demo parent > child" id="btn2" /><br />
<input type="button" value="Demo prev + next" id="btn3" /><br />
<input type="button" value="Demo prev ~ siblings" id="btn4" /><br />
</form>
<script type="text/javascript"><!--
$(document).ready(function(){
	var ck='blue';
	$('#btn1').click(function(){
		$('#msg_div').text($.trim($('form[name=f1] input[name=text1]').val()));
	});
	$('#btn2').click(function(){
		$('#msg_div').text($.trim($('form[name=f1] > span > input[name=text2]').val()));
	});
	$('#btn3').click(function(){
		if(ck=='blue'){
			$('span+input').css("color", "yellow");
			ck='yellow';
		}
		else{
			$('span+input').css("color", "blue");
			ck='blue';

		}
	});
	$('#btn4').click(function(){
		$('span ~ input[id=btn1]').css('color','green');
	});
});
// --></script>
</body>
</html>


jQuery 1.3.1 Ajax Cross Domain JSP Demo

23 二月, 2009

一般Ajax 是無法跨網域呼叫的,不過有兩個方法可以逹到跨網域呼叫:

&#x10;

      &#x10;

    1. 使用伺服器端讀取遠端檔案

&#x10;

  • 使用<script src="remote file">的語法

 

&#x10;

&#x10;

第一種方法很簡單,用java.net.URL寫一個取得遠端content的proxy即可。

&#x10;

第二種方法,jQuery已將<script src="remote file">包裝在$.ajax之內,
並在 jQuery 的 API 內就有 demo 了,而使用的限制就是遠端回傳的content必需是jsonp的物件。

&#x10;

jQuery知道jsonp必需回傳一個function name,在本地端也必需有相對應的function name,

&#x10;

寫程式最麻煩的就是命名了,所以jQuery在$.getJSON(url,function(data){})的url參數內幫我們把call back所需的命名做了簡易的處理,
當jQuery在url參數內找到xxx=?這個參數,便會自動幫我們產生一個獨特的function name,
並將function name賦值給xxx傳出,此function name在本地端也會有實體化,
就是function(data){}的這個隱名函式,所以當我們使用$.getJSON這個方法時,
不用再為每個jsonp的call back想一個名字,實在方便很多,請看以下的簡易範例程式碼:

&#x10;

&#x10;

呼叫端(the_url 請改為您自己的server):

&#x10;

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">&#x10;<html xmlns="http://www.w3.org/1999/xhtml">&#x10;<head>&#x10;<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />&#x10;<title>jsonp</title>&#x10;<mce:script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" mce_src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></mce:script>&#x10;<mce:script type="text/javascript">&#x10;$(document).ready(function(){&#x10;	var the_url='http://www.yahoo.tw/json.jsp?callback=?';&#x10;	$.getJSON(the_url,function(data){&#x10;		$('#demo_content').html('<a href="'+data.link+'" mce_href="'+data.link+'" target="_blank">'+data.title+'</a><br />\n');&#x10;		for(var i=0;i<data.items.length;i++){&#x10;			$('#demo_content').append('feed:<a href="'+data.items[i].link+'" mce_href="'+data.items[i].link+'" target="_blank">'+data.items[i].title+'</a><br />\n');&#x10;		}&#x10;	});&#x10;});&#x10;</mce:script>&#x10;</head>&#x10;<body>&#x10;<div id="demo_content"></div>&#x10;</body>&#x10;</html>

&#x10;

&#x10;

&#x10;

遠端被呼叫端:

&#x10;

&#x10;

<%@page contentType="application/x-javascript; charset=utf-8" errorPage=""%>&#x10;<%&#x10;out.clear();&#x10;String callback=(request.getParameter("callback")==null)?"":request.getParameter("callback");&#x10;//callback的開頭必須為英文字元,整個callback只允許長度25的英數字元&#x10;if(!callback.matches("^[a-zA-Z]{1}[a-zA-Z0-9]{0,24}$")){&#x10;	//所有不符合規則的callback都設為空字串,以免javascript發生錯誤&#x10;	callback="";&#x10;}&#x10;pageContext.setAttribute("callback",callback);&#x10;%>${callback}({&#x10;  "title": "walkOne - RSS 2.0",&#x10;  "link": "http://www.walkone.tw/",&#x10;  "description": "walkOne - RSS 2.0",&#x10;  "modified": "2009-02-23T09:55:26Z",&#x10;  "generator": "http://www.walkone.tw/",&#x10;  "items": [{&#x10;    "title": "開發者日誌",&#x10;    "link": "http://www.walkone.com.tw/blog/index.jsp?user_id=test"&#x10;  }]&#x10;})

&#x10;

jQuery UI 1.6rc6 內的 CSS class 建立 button

13 二月, 2009

//找出所有<button >btn</button>
$.each($('button.btn'),function(){
$(this).removeClass('btn');
$(this).addClass('ui-state-default ui-corner-all');
$(this).hover(
function() { $(this).addClass('ui-state-hover'); },
function() { $(this).removeClass('ui-state-hover'); }
);
});