知識社群登入
位置: Web Programming > 文件區 > Javascript > jQuery
jQuery 官網的筆記整理
by 蘇德宙, 2010-04-08 18:55, 人氣(1416)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="
jquery.js"></script>
<script>
  $(document).ready(
    function() {
      $("a").click(
        function(event) {
          alert("prevent the default behaviour");
          event.preventDefault();
        }
      );
    }
  );
</script>
</head>
<body>
    <a href="
jQueryhttp://jquery.com/">jQuery</a>
</body>
</html>
 
by 蘇德宙, 2010-04-08 19:40, 人氣(1450)
<style>
    a.test { font-weight: bold; }
</style>
 
$("a").addClass("test");
$("a").removeClass("test");
by 蘇德宙, 2011-01-29 12:42, 人氣(1309)
$("a").click(
  function(event){
    event.preventDefault();
    $(this).hide("slow");
  }
 
by 蘇德宙, 2010-04-10 10:36, 人氣(1413)
 
Select an Item
 
.name // css class
#name // element ID
name  // element
 
EX.
var v = $('#myDivId').val();          // get the value of an element
$('#myDivId').val("hello world");     // set the value of an element
 
is(expr) / hasClass(className)
if ($('#myDiv').is('.pretty')) $('#myDiv').show();
if ($('#myDiv').is(':hidden'))
 
Test whether an element exists using 'length'
if ( $('#myDiv').length ) $('#myDiv').show();
 
Handle a form element
$('#x').attr('disabled', true);  // or false
$("#x").removeAttr('disabled');
 
// checkbox
$('#x').attr('checked', true);
 
// selection
<select id="myselect">
  <option value="1" selected>Mr</option>
  <option value="2">Ms</option>
</select>
$("#myselect").val();                  // selected value, = 1
$("#myselect option:selected").text(); // Mr
 
Select an element at index n - .eq(n)
<ul>
  <li><a>item 1</a></li>
  <li><a>item 2</a></li>
</ul>
$(this).find('li a').eq(1).text(); // item 2