jQuery select() method triggers when a text in the text field or text area is selected. This method attaches a handler, which executes when the selected event is fired.
当在文本字段或文本区域中选择文本时, jQuery select()方法触发。 此方法附加一个处理程序,该处理程序在激发选定事件时执行。
The syntax for using jQuery select():
使用jQuery select()的语法:
- select() 选择()
This signature is used without any arguments
使用此签名时不带任何参数
- select (handler) 选择(处理程序)
The handler is a function, which is executed when the text inside the <text area> or <input type=”text”> gets selected.
处理程序是一个函数,当选择<文本区域>或<输入类型=“文本”>中的文本时执行。
jQuery select()函数示例 (jQuery select() function example)
Following example demonstrates select() method usage.
下面的示例演示select()方法的用法。
jquery-select.html
jquery-select.html
<!DOCTYPE html>
<html>
<head>
<title>jQuery Select</title>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
<style>
p {
color: green;
}
div {
color: red;
}
</style>
</head>
<body>
<p>Select Event Demo </p>
<input type="text" value="Select this text">
<div></div>
<script>
$( ":input" ).select(function() {
$( "div" ).text( "You have selected the text." ).show().fadeOut( 1500 );
});
</script>
</body>
</html>
In this example, you can see that the select() method is triggered when you select the text in the input field. When you select the text, the function attached to the select() method is executed and a text is displayed below the input field and fades out. This is how select() method works in the jquery.
在此示例中,您可以看到在输入字段中选择文本时触发了select()方法。 选择文本时,将执行附加到select()方法的函数,并且文本将在输入字段下方显示并逐渐消失 。 这就是select()方法在jquery中的工作方式。
You can try it yourself by selecting the text in the below text box.
您可以通过在下面的文本框中选择文本来自己尝试。
翻译自: https://www.journaldev.com/5024/jquery-select-text-event-function-select-method