Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,025

HOME > jQuery > jQuery Introduction > jQuery Selectors : jQuery Selectors and Element



Clound SSD Virtual Server

jQuery Selectors : jQuery Selectors and Element

jQuery Selectors รูปแบบคำสั่งการอ้างถึง element ของ jQuery Library Framework
jQuery Selectors ความสามารถของ jQuery ที่ได้รับการยอมรับว่ายอดเยี่ยมที่สุด ก็คือการ Selectors อ้างถึง element และจัดการกับกลุ่มของ element ได้อย่างมีประสิทธิภาพ โดยสามารถเข้าถึง elemment ถึงประดับองค์ประกอบ Attribute ของ element สามารถเรียกจากหนึ่งหรือว่าจัดการในระดับกลุ่มของ element ที่อยู่ในชนิดเดียวกันได้ หรือระดับที่อยู่ลึกเข้าไปใน element ย่อยก็สามารถอ้างถึงได้เช่นเดียวกัน ซึ่งจะเป็นประโยชน์มาก ๆ ในการนำคุณสมบัติเหล่านี้เข้ามาช่วยพัฒนาโปรแกรม และ จัดการกับ element ต่าง ๆ หน้าใน Web Page ได้อย่างมีประสิทธิภาพ และการเขียน Code อันสั้น ๆ กับ Syntax ที่ง่าย ๆ ถือว่ายอดเยี่ยมที่สุด

รูปแบบการอ้างถึง element Selectors Syntax
$(selector).action()


ตัวอย่างการใช้งาน Selectors ในการอ้างถึง element

1. $(this).action()
ใช้ Selectors อ้างถึง element ใน ปัจจุบัน

Code (jQuery)
<html>
<head>
<title>ThaiCreate.Com jQuery Tutorials</title>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#btnClick").click(function(){
  $(this).val("Hello");
  });
});
</script>
</head>
<body>
<input type="button" id="btnClick" value="Click">
</body>
</html>


Screenshot

jQuery Syntax

คำอธิบาย
จากตัวอย่างบรรทัด $("#btnClick").click(function() มีการอ้างถึง element id="btnClick" ของปุ่ม button โดยเมื่อ event มีการ click จะเปลี่ยนข้อความใน button เป็นคำว่า Hello



2. $("#test").action()
ใช้ Selectors อ้างถึง element ที่อยู่ภายใต้ id="test"

Code (jQuery)
<html>
<head>
<title>ThaiCreate.Com jQuery Tutorials</title>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript">
$(document).ready(function(){

  $("#btnText").click(function(){
	$("#txt1").val($("#btnText").val());
  });

  $("#btnClear").click(function(){
	$("#txt1").val('');
  });

});
</script>
</head>
<body>

<input type="text" id="txt1">

<input type="button" id="btnText" value="input text = 123 ">
<input type="button" id="btnClear" value="Clear">
</body>
</html>


Screenshot

jQuery Syntax

คำอธิบาย
จากตัวอย่างมีการเรียกใช้ $("#txt1") จาก id="txt1" โดยเมื่อคลิกที่ปุ่มของ button จะมีการ input ค่าและ clear ค่า








3. $("p").action()
ใช้ Selectors อ้างถึง element ที่อยู่ภายใต้ แทกทั้งหมด <p>หรือ <div> หรือ <span>

Code (jQuery)
<html>
<head>
<title>ThaiCreate.Com jQuery Tutorials</title>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript">
$(document).ready(function(){

  $("#btnHide").click(function(){
	$("p").hide();
	$("div").hide();
  });

  $("#btnShow").click(function(){
	$("p").show();
	$("div").show();
  });

});
</script>
</head>
<body>

<p>string paragraph string paragraph string paragraph</p>
<div>string div string div string div</div>

<input type="button" id="btnHide" value="Hide">
<input type="button" id="btnShow" value="Show">
</body>
</html>


Screenshot

jQuery Syntax

คำอธิบาย
จากตัวอย่างมีการเรียกใช้ $("#btnHide") จาก id="btnHide" และ $("p") จาก <p> โดยเมื่อคงิกที่ปุ่มจะสามารถเลือกให้ซ่อนหรือแสดงข้อความได้



4. $(".test").action()
ใช้ Selectors อ้างถึง element ที่อยู่ภายใต้ แทกทั้งหมด ที่เรียกใช้ css style sheet เช่น class="test"

Code (jQuery)
<html>
<head>
<title>ThaiCreate.Com jQuery Tutorials</title>
<style type="text/css"> 
	.css1 {
		font-size: 11px;
		font-weight: bold;
		color: #000000;
	}
	.css2 {
		font-size: 20;
		font-weight: bold;
		color: #FF0000;
	}
</style>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript">
$(document).ready(function(){

  $("#btnRemoveCss").click(function(){
    $(".css2").removeClass();
  });

  $("#btnAddCss").click(function(){
    $(".css1").addClass("css2");
  });

});
</script>
</head>
<body>

<p class="css1"> Welcome to jQuery on ThaiCreate.Com Tutorials</p>
<p class="css2"> Version 2011</p>

<input id="btnRemoveCss" type="button" value="Remove Css">
<input id="btnAddCss" type="button" value="Add Css">
</body>
</html>


Screenshot

jQuery Syntax

คำอธิบาย
จากตัวอย่างมีการเรียกใช้ $(".css1") และ $(".css2") เพื่ออ้างถึง element ที่มี Style Sheet ว่า class="css1" และ class="css2" และมีการ Remove Css และ Add Css เข้าไปใหม่



เพิ่มเติม
จากตัวอย่าง 3-4 ตัวอย่างข้างต้น จะเป็นการใช้งาน Selectors อ้างถึง element ที่ใช้งานและพบเจอเป็นประจำ นอกจากนี้ใน jQuery ยังมีรูปแบบการเรียกใช้งาน Selectors ที่หลากหลายมาก แทบจะไดว่าสามารถเรีกได้ถึงจะดับ Child ที่อยู่ภายใต้ elememt ต่างได้อีกด้วย

เช่น
$("p#p1")


จะเป็นการเรียก Selectors อ้างถึง id="p1" อยู่อย่ภายใน <p>...</b> ดูตัวอย่างเพื่อความเข้าใจ


Code (jQuery)
<html>
<head>
<title>ThaiCreate.Com jQuery Tutorials</title>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript">
$(document).ready(function(){

  $("#btnHide").click(function(){
	$("p#p1").hide();
  });

  $("#btnShow").click(function(){
	$("p#p1").show();
  });

});
</script>
</head>
<body>

<p id="p1">string paragraph string paragraph string paragraph</p>
<p>string div string div string div</p>

<input type="button" id="btnHide" value="Hide">
<input type="button" id="btnShow" value="Show">
</body>
</html>


Screenshot

jQuery Syntax

คำอธิบาย
จากตัวอย่างมีการเรียกใช้ $("p#p1") ซึ่งจะอ้างถึง <p id="p1"> เท่านั้น










การเรียกใช้งาน Selectors อื่น ๆ
$('div#mydiv.myclass') อ้างถึง div ชื่อ id="mydiv" และ css="myclass"
$('div#mydiv1 , div#mydiv1') อ้างถึง div ชื่อ id="mydiv1" , และ div ชื่อ id="mydiv2" (กรณีที่อ้างถึงหลายตัว)
$('div#mydiv1 , p#myp1') อ้างถึง div ชื่อ id="mydiv1" , และ p ชื่อ id="myp1" (กรณีที่อ้างถึลหลายตัว)
$('div.myclass') อ้างถึง div ที่ css="myclass"
$('div#imydiv1:first") อ้างถึง div ที่ id="mydiv1" ที่อยู่ลำดับแรก (ในกรณีที่มี id="mydiv1" หลายตัว)


สำหรับการใช้งาน Selectors เพื่ออ้างถึง element อื่น ๆ สามารถศึกษาได้จากหัวข้อถัดไป


หัวข้อการใช้งาน jQuery กับ Selectors ต่าง ๆ
Go to : jQuery Selectors : jQuery and Selectors การเรียกใช้งาน Selectors ของ jQuery ในการอ้างถึง element ต่าง ๆ

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2011-09-21 20:29:11 / 2017-03-19 13:15:17
  Download : No files
 Sponsored Links / Related

 
jQuery : What's a jQuery , jQuery คืออะไร ??
Rating :

 
jQuery : How to use , จะเขียน jQuery จะต้องทำอย่างไร
Rating :

 
jQuery Syntax : jQuery Basic Syntax
Rating :

 
jQuery HTML : jQuery and HTML
Rating :

 
jQuery Css : jQuery and Css
Rating :

 
jQuery Events : jQuery and Events
Rating :

 
jQuery Effects : jQuery and Effects
Rating :

 
jQuery Ajax : jQuery and Ajax
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่