jQuery Button 組件可以增強(qiáng)表單(Form) 中的 Buttons,Inputs 和 Anchor元素,使其具有按鈕顯示風(fēng)格,能夠正確對(duì)鼠標(biāo)滑動(dòng)做出反應(yīng)。
下例顯示把表單中的 button,input 和 anchor 元素都變?yōu)榘粹o風(fēng)格的 jQuery Button 組件。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Demos</title>
<link rel="stylesheet" href="themes/trontastic/jquery-ui.css" />
<script src="scripts/jquery-1.9.1.js"></script>
<script src="scripts/jquery-ui-1.10.1.custom.js"></script>
<script>
$(function () {
$("input[type=submit], a, button")
.button()
.click(function (event) {
event.preventDefault();
});
});
</script>
</head>
<body>
<button>A button element</button>
<input type="submit" value="A submit button" />
<a href="#">An anchor</a>
</body>
</html>
http://wiki.jikexueyuan.com/project/jquery-tutorial/images/28.png" alt="" />
除了支持基本的按鈕外,jQuery Button 組件可以把類型為 checkbox 的 input 元素變?yōu)榘粹o,這種按鈕可以有兩種狀態(tài),原態(tài)和按下?tīng)顟B(tài)。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Demos</title>
<link rel="stylesheet" href="themes/trontastic/jquery-ui.css" />
<script src="scripts/jquery-1.9.1.js"></script>
<script src="scripts/jquery-ui-1.10.1.custom.js"></script>
<script>
$(function () {
$("input[type=submit], a, button")
.button()
.click(function (event) {
event.preventDefault();
});
});
</script>
</head>
<body>
<button>A button element</button>
<input type="submit" value="A submit button" />
<a href="#">An anchor</a>
</body>
</html>
http://wiki.jikexueyuan.com/project/jquery-tutorial/images/29.png" alt="" />
按鈕也可以添加圖標(biāo),可以支持多個(gè)圖標(biāo),分別使用 primary 和 secondary 來(lái)指明。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Demos</title>
<link rel="stylesheet" href="themes/trontastic/jquery-ui.css" />
<script src="scripts/jquery-1.9.1.js"></script>
<script src="scripts/jquery-ui-1.10.1.custom.js"></script>
<script>
$(function () {
$("button:first").button({
icons: {
primary: "ui-icon-locked"
},
text: false
}).next().button({
icons: {
primary: "ui-icon-locked"
}
}).next().button({
icons: {
primary: "ui-icon-gear",
secondary: "ui-icon-triangle-1-s"
}
}).next().button({
icons: {
primary: "ui-icon-gear",
secondary: "ui-icon-triangle-1-s"
},
text: false
});
});
</script>
</head>
<body>
<button>Button with icon only</button>
<button>Button with icon on the left</button>
<button>Button with two icons</button>
<button>Button with two icons and no text</button>
</body>
</html>
http://wiki.jikexueyuan.com/project/jquery-tutorial/images/30.png" alt="" />
同樣,jQuery 也把 type 為 radio 的一組 Radio 按鈕構(gòu)成一組單選鈕,使用.buttonset 將多個(gè)單選鈕定義為一個(gè)組,其中只有一個(gè)可以是選中狀態(tài)。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Demos</title>
<link rel="stylesheet" href="themes/trontastic/jquery-ui.css" />
<script src="scripts/jquery-1.9.1.js"></script>
<script src="scripts/jquery-ui-1.10.1.custom.js"></script>
<script>
$(function () {
$("#radio").buttonset();
});
</script>
</head>
<body>
<form>
<div id="radio">
<input type="radio" id="radio1" name="radio" />
<label for="radio1">Choice 1</label>
<input type="radio" id="radio2" name="radio" checked="checked" />
<label for="radio2">Choice 2</label>
<input type="radio" id="radio3" name="radio" />
<label for="radio3">Choice 3</label>
</div>
</form>
</body>
</html>
http://wiki.jikexueyuan.com/project/jquery-tutorial/images/31.png" alt="" />