2013年4月12日 星期五

[JAVA_Spring]建立一個基本簡單的Spring project



1、本文中使用的相關技術

Eclipse 4.2 Version: Juno Service Release 2
Maven 3.0.5
JAVA JDK 1.6.0_41
Spring 3.0.5.RELEASE


2、建立一個Maven專案
      使用Eclipse創建
      File -> New -> Other (快速鍵Ctrl+ N)

選擇Maven -> Maven Project

本文使用  創建一個簡單的專案 ->下一步

輸入專案名稱



3、加入 Spring 3.0 jar
      修改在Maven專案中的pom.xml 
   位置如圖

在pom.xml 的 <description>springDemo_1</description>下 加入以下的xml



程式碼:

<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>

<dependencies>

<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

</dependencies>



如下圖


 
4、使用Spring bean

      建立Spring bean 檔案,本文名稱為 HelloWorld.java。
      目錄在springDemo_1/src/main/java/com/demo1/HelloWorld.java

程式碼:
  package com.demo1;

  /**
  * Spring HelloWorld bean
  * 
  */
  public class HelloWorld {

   private String name;

   public void setName(String name) {
    this.name = name;
   }

   public void printHello() {
    System.out.println("Spring 3 : Hello ! " + name);
   }
  }


5、建立Spring bean config file
      建立Spring bean xml  檔案,本文名稱為 SpringBeans.xml。
      目錄在springDemo_1/src/main/resources/SpringBeans.xml

      
     程式碼:
     <?xml version="1.0" encoding="UTF-8" ?>
     <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloBean" class="com.demo1.HelloWorld">
<property name="name" value="mySpring"/>
</bean>
   </beans>


     




6、建立執行入口main
建立java main, 本文名稱為 MyMain.java。
目錄在springDemo_1/src/main/java/com/demo1/MyMain.java

                加入程式碼:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
           

程式碼:

  package com.demo1;

  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;

  public class MyMain {

   /**
    * @param args
    */
   public static void main(String[] args) {
    // TODO Auto-generated method stub
    ApplicationContext context = new ClassPathXmlApplicationContext(
      "SpringBeans.xml");
   
    HelloWorld obj = (HelloWorld) context.getBean("helloBean");
    obj.printHello();
   }

  }



7、目前的專案結構




8. 執行
在專案上按右鍵-> Run As -> Java Application


9. 結果
    查看console 是否有顯示你的Hello


10、主要學習


本文主要學習,建立一個簡單的spring專案。

本文中程式中使用到二個srping class

org.springframework.context.ApplicationContext;
org.springframework.context.support.ClassPathXmlApplicationContext;

你可以參考:
spring 3.0 api
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/

回 JAVA Spring 目錄 查看相關資訊




2013年4月10日 星期三

[jQuery mobile]jquery mobile 使用 Create 及refresh




1、Create與及refresh:

創建事件和刷新方法有一個重要的區別需要注意
重要區別在一些widgets關系。
Create - 事件適合用於加強原始標記包含一個或多個widgets。
refresh - 方法應該使用現有的widgets(已增強)已編程操作和需要更新匹配的UI。

例如:
如果你有一個頁面,
在那裡你動態地追加一個新的無序列表與數據的 data-role=listview 屬性創建頁面後,
觸發父元素上創建,列表將它改造成一個ListView風格的小部件。
如果多個列表項,然後以編程方式添加,
調用ListView的刷新方法將更新這些新的列表項的強化狀態,
並保留現有的列表項目不變。



2、範例:
2.1 流程:
2.1.1、主頁面loginMember.html ,進入會有二個選項,為Test create 及Test refresh。
2.1.2、選擇Test create ,是將div id為#divData,載入createpage.html #data。
主要是測試,將更新大量的元件。
2.1.3、選擇Test refresh,是將div id為#divData,載入refreshpage.html #data。
主要是測試,將更新大量的元件後,在於其中更新部分元件。

  如果將移除
  $('#divData').trigger('create');
  $('#divData').trigger('create').trigger('pagecreate');
  其你更新的大量元件將沒有jQuery ui的效果。
2.2 程式
主頁面:
loginMember.html
<!DOCTYPE html>
<html>
<head>
<title>jQuery Mobile Menu Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta id="extViewportMeta" name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>

<script type="text/javascript">
$().ready(function(){
$("#createpage").live("click",function(){
$("#divData").empty();
$("#divData").load("createpage.html #data" , function() {$('#divData').trigger('create'); });
});
$("#refreshpage").live("click",function(){
$("#divData").empty();
$("#divData").load("refreshpage.html #data" , function() {$('#divData').trigger('create').trigger('pagecreate'); });
});
});

$('#listDemo').live('pagecreate', function() {
$('#withoutRefresh').on("click", function() {
$('ul').append('<li>list item</li>');
});
$('#withRefresh').on("click", function()  {
$('ul').append('<li>list item</li>');
$('ul').listview('refresh');
});
$('#clear').on("click", function()
{
$('ul').empty();
});
});
</script>
</head>
<body>
<div id="listDemo" data-role="page">
<div data-role="header">
<h3><a href="#"id="createpage">Test create</a></h3>
<h3><a href="#"id="refreshpage">Test refresh</a></h3>
</div>

<div data-role="content" id="divData" align="left">
</div>
</div>
</body>
</html>

測試create 頁面:
createpage.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div id="data">
<div data-role="main" align="center">請選擇</div>
<div data-role="footer">
<div data-role="navbar">
<ul>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">6</a></li>
</ul>
</div>
</div>
<div><label for='account1'>帳號:</label><input type='text' /></div>
</div>
</body>
</html>

測試refresh 頁面:
refreshpage.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div id="data">
<input type="button" id="withoutRefresh" value="Add without refresh"/>
<input type="button" id="withRefresh" value="Add with refresh"/>
<ul data-role="listview" data-inset="true"></ul>
<input type="button" id="clear" value="Clear List"/>
</div>
 </body>
</html>

3、畫面
程式執行畫面
按下 Test create 的畫面
按下Test refresh
按下Test refresh->Add Without refresh
按下Test refresh->Add with refresh



4、參考
本文編輯的版本為jQuery.mobile-1.3

參考來源:

jQuery mobile Scripting pages
http://jquerymobile.com/demos/1.2.1/docs/pages/page-scripting.html

Operation Mobile
http://operationmobile.com/category/jquery-mobile/


jquery moblie download
http://jquerymobile.com/download/






2013年4月2日 星期二

[jQuery Mobile]好站分享


Jquery Mobile 官網(英)


jQuery Mobile - Web Deliver for All








[jQuery]jQuery Selector 之表單對象屬性類



jQuery Selector 之表單對象屬性類
:enabled
:disabled
:checked
:selected


1、
:enabled
匹配所有可用元素


程式範例:

<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單對象屬性類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:enabled 匹配所有可用元素
//查找所有可用的input元素
var str=' ';//此變數顯示訊息用的
$("input:enabled").val(function() {
str += '-- name: ' + this.name;
});
alert(str);
});
</script>
</head>
<body>
<form>
 <input name="email" disabled="disabled" />
 <input name="id" />
</form>
</body>
</html>


顯示結果:



2、
:disabled
匹配所有不可用元素


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單對象屬性類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:disabled 匹配所有不可用元素
//查找所有不可用的input元素
var str=' ';//此變數顯示訊息用的
$("input:disabled").val(function() {
str += '-- name: ' + this.name;
});
alert(str);
});
</script>
</head>
<body>
<form>
 <input name="email" disabled="disabled" />
 <input name="id" />
</form>
</body>
</html>



顯示結果:



3、
:checked
匹配所有選中的被選中元素(複選框、單選框等,不包括select中的option)


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單對象屬性類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:checked 匹配所有選中的被選中元素(複選框、單選框等,不包括select中的option)
//查找所有選中的複選框元素
var str=' ';//此變數顯示訊息用的
$("input:checked").val(function() {
str += '-- value: ' + this.value;
});
alert(str);
});
</script>
</head>
<body>
<form>
 <input type="checkbox" name="newsletter" checked="checked" value="Daily" />
 <input type="checkbox" name="newsletter" value="Weekly" />
 <input type="checkbox" name="newsletter" checked="checked" value="Monthly" />
</form>
</body>
</html>



顯示結果:



4、
:selected
匹配所有選中的option元素


程式範例:

<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單對象屬性類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:selected 匹配所有選中的option元素
//查找所有選中的選項元素
var str=' ';//此變數顯示訊息用的
$("select option:selected").val(function() {
str += '-- value: ' + this.value;
});
alert(str);
});
</script>
</head>
<body>
<select>
 <option value="1">Flowers</option>
 <option value="2" selected="selected">Gardens</option>
 <option value="3">Trees</option>
</select>
</body>
</html>


顯示結果:




[jQuery]jQuery Selector 之表單類


jQuery Selector 之表單類
:input
:text
:password
:radio
:checkbox
:submit
:image
:reset
:button
:file
:hidden


1、
:input
匹配所有input, textarea, select 和button 元素


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:input 匹配所有input, textarea, select 和button 元素
//查找所有的input元素
var str=' ';//此變數顯示訊息用的
$(":input").val(function() {
str += '-- value: ' + this.value;
});
alert(str);
});
</script>
</head>
<body>
<form>
<!-- 下面這些元素都會被匹配到。 -->
    <input type="button" value="Input Button"/>
    <input type="checkbox" value="Input checkbox"/>

    <input type="file" value="Input file"/>
    <input type="hidden" value="Input hidden"/>
    <input type="image" value="Input image"/>

    <input type="password" value="Input password"/>
    <input type="radio" value="Input radio"/>
    <input type="reset" value="Input reset"/>

    <input type="submit" value="Input submit"/>
    <input type="text" value="Input text"/>
    <select><option>Option</option></select>

    <textarea></textarea>
    <button>Button</button>

</form>
</body>
</html>



顯示結果:



2、
:text
匹配所有的單行文本框


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:text 匹配所有的單行文本框
//查找所有的text元素
var str=' ';//此變數顯示訊息用的
$(":text").val(function() {
str += '-- value: ' + this.value;
});
alert(str);
});
</script>
</head>
<body>
<form>
    <input type="button" value="Input Button"/>
    <input type="checkbox" value="Input checkbox"/>

    <input type="file" value="Input file"/>
    <input type="hidden" value="Input hidden"/>
    <input type="image" value="Input image"/>

    <input type="password" value="Input password"/>
    <input type="radio" value="Input radio"/>
    <input type="reset" value="Input reset"/>

    <input type="submit" value="Input submit"/>
    <input type="text" value="Input text"/>
    <select><option>Option</option></select>

    <textarea></textarea>
    <button>Button</button>

</form>
</body>
</html>



顯示結果:



3、
:password
匹配所有密碼框


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:password 匹配所有密碼框
//查找所有密碼框
var str=' ';//此變數顯示訊息用的
$(":password").val(function() {
str += '-- value: ' + this.value;
});
alert(str);
});
</script>
</head>
<body>
<form>
    <input type="button" value="Input Button"/>
    <input type="checkbox" value="Input checkbox"/>

    <input type="file" value="Input file"/>
    <input type="hidden" value="Input hidden"/>
    <input type="image" value="Input image"/>

    <input type="password" value="Input password"/>
    <input type="radio" value="Input radio"/>
    <input type="reset" value="Input reset"/>

    <input type="submit" value="Input submit"/>
    <input type="text" value="Input text"/>
    <select><option>Option</option></select>

    <textarea></textarea>
    <button>Button</button>

</form>
</body>
</html>



顯示結果:



4、
:radio
匹配所有單選按鈕


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:radio 匹配所有單選按鈕
//查找所有單選按鈕
var str=' ';//此變數顯示訊息用的
$(":radio").val(function() {
str += '-- value: ' + this.value;
});
alert(str);
});
</script>
</head>
<body>
<form>
    <input type="button" value="Input Button"/>
    <input type="checkbox" value="Input checkbox"/>

    <input type="file" value="Input file"/>
    <input type="hidden" value="Input hidden"/>
    <input type="image" value="Input image"/>

    <input type="password" value="Input password"/>
    <input type="radio" value="Input radio"/>
    <input type="reset" value="Input reset"/>

    <input type="submit" value="Input submit"/>
    <input type="text" value="Input text"/>
    <select><option>Option</option></select>

    <textarea></textarea>
    <button>Button</button>

</form>
</body>
</html>



顯示結果:



5、
:checkbox
匹配所有復選框


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:checkbox 匹配所有復選框
//查找所有復選框
var str=' ';//此變數顯示訊息用的
$(":checkbox").val(function() {
str += '-- value: ' + this.value;
});
alert(str);
});
</script>
</head>
<body>
<form>
    <input type="button" value="Input Button"/>
    <input type="checkbox" value="Input checkbox"/>

    <input type="file" value="Input file"/>
    <input type="hidden" value="Input hidden"/>
    <input type="image" value="Input image"/>

    <input type="password" value="Input password"/>
    <input type="radio" value="Input radio"/>
    <input type="reset" value="Input reset"/>

    <input type="submit" value="Input submit"/>
    <input type="text" value="Input text"/>
    <select><option>Option</option></select>

    <textarea></textarea>
    <button>Button</button>

</form>
</body>
</html>



顯示結果:



6、
:submit
匹配所有提交按鈕


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:submit 匹配所有提交按鈕
//查找所有提交按鈕
var str=' ';//此變數顯示訊息用的
$("input:submit").val(function() {
str += '-- value: ' + this.value;
});
alert(str);
});
</script>
</head>
<body>
<form>
    <input type="button" value="Input Button"/>
    <input type="checkbox" value="Input checkbox"/>

    <input type="file" value="Input file"/>
    <input type="hidden" value="Input hidden"/>
    <input type="image" value="Input image"/>

    <input type="password" value="Input password"/>
    <input type="radio" value="Input radio"/>
    <input type="reset" value="Input reset"/>

    <input type="submit" value="Input submit"/>
    <input type="text" value="Input text"/>
    <select><option>Option</option></select>

    <textarea></textarea>
    <button>Button</button>

</form>
</body>
</html>



顯示結果:



7、
:image
匹配所有圖像域


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:image 匹配所有圖像域
//查找所有圖像域
var str=' ';//此變數顯示訊息用的
$(":image").val(function() {
str += '-- value: ' + this.value;
});
alert(str);
});
</script>
</head>
<body>
<form>
    <input type="button" value="Input Button"/>
    <input type="checkbox" value="Input checkbox"/>

    <input type="file" value="Input file"/>
    <input type="hidden" value="Input hidden"/>
    <input type="image" value="Input image"/>

    <input type="password" value="Input password"/>
    <input type="radio" value="Input radio"/>
    <input type="reset" value="Input reset"/>

    <input type="submit" value="Input submit"/>
    <input type="text" value="Input text"/>
    <select><option>Option</option></select>

    <textarea></textarea>
    <button>Button</button>

</form>
</body>
</html>



顯示結果:



8、
:reset
匹配所有重置按鈕


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:reset 匹配所有重置按鈕
//查找所有重置按鈕
var str=' ';//此變數顯示訊息用的
$(":reset").val(function() {
str += '-- value: ' + this.value;
});
alert(str);
});
</script>
</head>
<body>
<form>
    <input type="button" value="Input Button"/>
    <input type="checkbox" value="Input checkbox"/>

    <input type="file" value="Input file"/>
    <input type="hidden" value="Input hidden"/>
    <input type="image" value="Input image"/>

    <input type="password" value="Input password"/>
    <input type="radio" value="Input radio"/>
    <input type="reset" value="Input reset"/>

    <input type="submit" value="Input submit"/>
    <input type="text" value="Input text"/>
    <select><option>Option</option></select>

    <textarea></textarea>
    <button>Button</button>

</form>
</body>
</html>


顯示結果:



9、
:button
匹配所有按鈕


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:button 匹配所有按鈕
//查找所有按鈕
var str=' ';//此變數顯示訊息用的
$(":button").val(function() {
str += '-- value: ' + this.value;
});
alert(str);
});
</script>
</head>
<body>
<form>
    <input type="button" value="Input Button"/>
    <input type="checkbox" value="Input checkbox"/>

    <input type="file" value="Input file"/>
    <input type="hidden" value="Input hidden"/>
    <input type="image" value="Input image"/>

    <input type="password" value="Input password"/>
    <input type="radio" value="Input radio"/>
    <input type="reset" value="Input reset"/>

    <input type="submit" value="Input submit"/>
    <input type="text" value="Input text"/>
    <select><option>Option</option></select>

    <textarea></textarea>
    <button value="Button">Button</button>

</form>
</body>
</html>



顯示結果:



10、
:file
匹配所有文件域


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:file 匹配所有文件域
//查找所有文件域
$(":file").css("background-color","red");
});
</script>
</head>
<body>
<form>
    <input type="button" value="Input Button"/>
    <input type="checkbox" value="Input checkbox"/>

    <input type="file" value="Input file"/>
    <input type="hidden" value="Input hidden"/>
    <input type="image" value="Input image"/>

    <input type="password" value="Input password"/>
    <input type="radio" value="Input radio"/>
    <input type="reset" value="Input reset"/>

    <input type="submit" value="Input submit"/>
    <input type="text" value="Input text"/>
    <select><option>Option</option></select>

    <textarea></textarea>
    <button>Button</button>

</form>
</body>
</html>



顯示結果:



11、
:hidden
匹配所有不可見元素,或者type為hidden的元素


程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之表單類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:hidden 匹配所有不可見元素,或者type為hidden的元素
//查找所有tr不可見元素
var str=' ';//此變數顯示訊息用的
$("tr:hidden").html(
function(n,value) {
str += ' value: ' + value;
});
alert(str);
});
</script>
</head>
<body>
<form>
<table>
 <tr style="display:none"><td>Value 1</td></tr>
 <tr><td>Value 2</td></tr>
</table>
</form>
</body>
</html>



顯示結果:




[jQuery]jQuery Selector 之子元素類


[jQuery]jQuery Selector 之子元素類

:nth-child
:first-child
:last-child
:only-child


1、
:nth-child
匹配其父元素下的第N個子或奇偶元素

':eq(index)' 只匹配一個元素,而這個將為每一個父元素匹配子元素。

:nth-child 從1開始的,而:eq()是從0算起的!
可以使用:
<br>nth-child(even)
<br>:nth-child(odd)
<br>:nth-child(3n)
<br>:nth-child(2)
<br>:nth-child(3n+1)
<br>:nth-child(3n+2)


程式範例:

<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之子元素類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:nth-child 匹配其父元素下的第N個子或奇偶元素
//在每個 ul 查找第 2個li
var str=' ';//此變數顯示訊息用的
$("ul li:nth-child(2)").html(
function(n,value) {
str += ' value: ' + value;
});
alert(str);
});
</script>
</head>
<body>
<ul>
 <li>John</li>
 <li>Karl</li>
 <li>Brandon</li>
</ul>
<ul>
 <li>Glen</li>
 <li>Tane</li>
 <li>Ralph</li>
</ul>
</body>
</html>


顯示結果:






2、
:first-child
匹配第一個子元素
':first'
只匹配一個元素,而此選擇符將為每個父元素匹配一個子元素

程式範例:

<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之子元素類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:first-child 匹配第一個子元素
//在每個 ul 中查找第一個 li
var str=' ';//此變數顯示訊息用的
$("ul li:first-child").html(
function(n,value) {
str += ' value: ' + value;
});
alert(str);
//':first' 只匹配一個元素,而此選擇符將為每個父元素匹配一個子元素
var str=' ';//此變數顯示訊息用的
$("ul li:first").html(
function(n,value) {
str += ' value: ' + value;
});
alert(str);
});
</script>
</head>
<body>
<ul>
 <li>John</li>
 <li>Karl</li>
 <li>Brandon</li>
</ul>
<ul>
 <li>Glen</li>
 <li>Tane</li>
 <li>Ralph</li>
</ul>
</body>
</html>


顯示結果:








3、
:last-child
匹配最後一個子元素

':last'
只匹配一個元素,而此選擇符將為每個父元素匹配一個子元素

程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之子元素類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:last-child 匹配最後一個子元素
//在每個 ul 中查找最后一個 li
var str=' ';//此變數顯示訊息用的
$("ul li:last-child").html(
function(n,value) {
str += ' value: ' + value;
});
alert(str);
//':last'只匹配一個元素,而此選擇符將為每個父元素匹配一個子元素
var str=' ';//此變數顯示訊息用的
$("ul li:last").html(
function(n,value) {
str += ' value: ' + value;
});
alert(str);
});
</script>
</head>
<body>
<ul>
 <li>John</li>
 <li>Karl</li>
 <li>Brandon</li>
</ul>
<ul>
 <li>Glen</li>
 <li>Tane</li>
 <li>Ralph</li>
</ul>
</body>
</html>



顯示結果:









4、
:only-child
如果某個元素是父元素中唯一的子元素,那將會被匹配

如果父元素中含有其他元素,那將不會被匹配。



程式範例:
<!DOCTYPE html>
<html>
<head>
<title>[jQuery]jQuery Selector 之子元素類</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>

<script>
$(document).ready(function(){
//:only-child 如果某個元素是父元素中唯一的子元素,那將會被匹配
//在 ul 中查找是唯一子元素的 li
var str=' ';//此變數顯示訊息用的
$("ul li:only-child").html(
function(n,value) {
str += ' value: ' + value;
});
alert(str);
});
</script>
</head>
<body>
<ul>
 <li>John</li>
 <li>Karl</li>
 <li>Brandon</li>
</ul>
<ul>
 <li>Glen</li>
</ul>
</body>
</html>



顯示結果:







標籤

Oracle (150) Oracle DB (144) Oracle_DB (143) Oracle SQL (135) JAVA (84) css-基本類 (65) MySQL (59) CSS Selector (58) jQuery (49) JavaScript-基本類 (39) Spring Boot (38) JavaScript (37) JavaScript HTML DOM (37) JavaScript-HTML_DOM (36) CSS3 (30) JAVA-基本類 (28) jQuery UI (27) Apache (23) Oracle GROUP BY (20) datepicker (20) Android (18) Oracle Date (17) c (17) JAVA-lang套件 (16) Linux (16) Oracle Sub Query (16) Spring-基本類 (16) jQuery-基本類 (16) MySQL-進階系列教學 (15) Android基本類 (14) Grails (14) Oracle join (14) SQLite (13) Spring (13) WIN7-基本類 (13) grails-基本類 (13) linux cent os (13) CKEditor (12) JAVA-流程控制類 (12) JAVA_Spring (12) PHP (11) Spring MVC (11) MySQL-基本系列教學 (10) Notepad (10) Notepad++ (10) SQLite for java (10) Windows (10) c/c++ (10) eclipse (9) jQuery-Selector (9) sqldeveloper (9) DB_Toad (8) JAVA_IDE_Eclipse (8) JavaScript-String類 (8) MySQL DB Toad (8) MySQL-DATE相關 (8) MySQL-函式相關 (8) Spring Bean (8) Android Studio (7) HTML5 (7) Hibernate (7) JAVA-OCWCD (7) JavaScript-陣列類 (7) Docker (6) JAVA-程式分享 (6) JAVA.util套件 (6) JavaScript-數學類 (6) MinGw (6) MySQL-其它類 (6) Servlet (6) centos (6) Apache_Tomcat (5) Apache套件_POI (5) CSS (5) JavaScript-Date物件 (5) JavaScript-其它類 (5) PostgreSQL (5) httpd (5) log4j (5) 基本資訊 (5) 開發工具 (5) CSS Properties (4) Dev-C++ (4) IntelliJ IDEA (4) Oracle DDL (4) Sublime (4) TortoiseSVN (4) apache_Maven (4) Android NDK (3) Eclipse IDE for C/C++ (3) Hibernate-基本類 (3) JAVA-問題 (3) JAVA-綀習分享 (3) JVM (3) Linux 指令 (3) Proxy Server (3) Spring Mobile (3) Spring web (3) Squid (3) VirtualBox (3) maven (3) zk (3) 生活其它 (3) Bootstrap (2) Filter (2) JAVA_IO (2) JAVA_其它_itext套件 (2) JBoss-問題 (2) JSP (2) Jboss (2) Listener (2) MySQL-語法快速查詢 (2) Spring AOP (2) Spring Batch (2) Spring Boot Actuator (2) Spring i18n (2) Subversive (2) Tomcat 8 (2) UML (2) WebJars (2) WinMerge (2) c++ (2) c語言綀習題 (2) jQuery Mobile (2) jQuery-事件處理 (2) jQuery-套件類 (2) putty (2) svn (2) weblogic (2) Apache_JMeter (1) Apache套件_BeanUtils (1) Apache套件_StringUtils (1) Base64 (1) Google API (1) HTML5-基本類 (1) Heap (1) JAVA 7 (1) JAVA SE 、JAVA EE、JAVA ME (1) JAVA 日期 (1) JAVA-OCJP (1) JAVA-WEB (1) JAVA_IDE (1) JAVA其它 (1) JBoss Server (1) JDK (1) JMX (1) JRE (1) Java RMI (1) Java String (1) Joda Time (1) Linux_其它 (1) MySQL教學 (1) Oracle_VirtualBox (1) SQL Server (1) SWT (1) Session (1) Stack (1) Struts 2 (1) Tool (1) ZK Studio (1) csv (1) grails-其它類 (1) jQuery-進階 (1) java mail (1) java web (1) java8 (1) jsoup (1) mockmvc (1) modules (1) tomcat (1) win10 (1) 其它類 (1) 圖片工具 (1) 模擬器 (1) 讀書分享 (1) 開發資訊 (1)

精選文章

初學 Java 的 HelloWorld 程式

撰寫一個JAVA程式 public class HelloWorld{ public static void main(String[ ] args){ System.out.println("我第一支Java程式!!"); } } ...