jQuery 官網的筆記整理
by 蘇德宙, 2010-04-08 18:55, 人氣(1580)
<!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>
<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>
[demo]
by 蘇德宙, 2010-04-08 19:40, 人氣(1599)
<style>
a.test { font-weight: bold; }
</style>
a.test { font-weight: bold; }
</style>
$("a").addClass("test");
$("a").removeClass("test");
by 蘇德宙, 2011-01-29 12:42, 人氣(1464)
by 蘇德宙, 2010-04-10 10:36, 人氣(1557)
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
$('#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
<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>
<li><a>item 1</a></li>
<li><a>item 2</a></li>
</ul>
$(this).find('li a').eq(1).text(); // item 2
by 蘇德宙, 2010-04-13 16:59, 人氣(1623)