.select()
.select( handler(eventObject) ) 返回: jQuery
描述: 為 "select" 事件綁定一個(gè)處理函數(shù),或者觸發(fā)元素上的 "select" 事件。
-
version added: 1.0.select( handler(eventObject) )
handler(eventObject)每次事件觸發(fā)時(shí)會(huì)執(zhí)行的函數(shù)。
-
version added: 1.4.3.select( [ eventData ], handler(eventObject) )
eventData將要傳遞給事件處理函數(shù)的數(shù)據(jù)映射。
handler(eventObject)每次事件觸發(fā)時(shí)會(huì)執(zhí)行的函數(shù)。
version added: 1.0.select()
這個(gè)函數(shù)的第一種用法是 .bind('select', handler)
的快捷方式,第二種用法是 .trigger('select')
的快捷方式。
當(dāng)用戶在一個(gè)元素中進(jìn)行文本選擇時(shí),select
事件就會(huì)被發(fā)送到這個(gè)元素。此事件被限制在<input type="text">
和<textarea>
。
舉例來說,請(qǐng)看下面的HTML:
<form> <input id="target" type="text" value="Hello there" /> </form> <div id="other"> Trigger the handler </div>
這個(gè)事件處理程序可以綁定到文本框
$('#target').select(function() { alert('Handler for .select() called.'); });
現(xiàn)在文本框中任何字符被選擇,警告將被顯示。僅僅設(shè)置插入點(diǎn)的位置將不會(huì)觸發(fā)該事件。我們可以通過點(diǎn)擊另一個(gè)因素來手動(dòng)觸發(fā)事件時(shí):
$('#other').click(function() { $('#target').select(); });
這些代碼執(zhí)行后,點(diǎn)擊觸發(fā)按鈕同樣警報(bào)顯示:
Handler for .select() called.
此外,默認(rèn)文本域上的select
動(dòng)作被解除,所以整個(gè)文本字段將被選中。
用于檢索當(dāng)前選定文本的方法在各個(gè)瀏覽器中是不同的。jQuery的一個(gè)插件都提供跨平臺(tái)的解決方案。
Examples:
Example: 在輸入框中文本被選中時(shí)做一件事情時(shí):
<!DOCTYPE html>
<html>
<head>
<style>
p { color:blue; }
div { color:red; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p>
Click and drag the mouse to select text in the inputs.
</p>
<input type="text" value="Some text" />
<input type="text" value="to test on" />
<div></div>
<script>
$(":input").select( function () {
$("div").text("Something was selected").show().fadeOut(1000);
});
</script>
</body>
</html>
Demo:
Example: To trigger the select event on all input elements, try:
$("input").select();