jQuery选择文本事件函数– select()方法

时间:2020-02-23 14:46:13  来源:igfitidea点击:

当在文本字段或者文本区域中选择文本时,jQuery select()方法触发。
此方法附加一个处理程序,该处理程序在激发选定事件时执行。

使用jQuery select()的语法:

  • select()

使用此签名时不带任何参数

  • select (handler)

该处理程序是一个函数,当<文本区域>或者<INPUT TYPE ="文本">内的文本被选中时执行。

jQuery select()函数示例

下面的示例演示select()方法的用法。

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>

在此示例中,您可以看到在输入字段中选择文本时触发了select()方法。
选择文本时,将执行附加到select()方法的函数,并且文本将显示在输入字段下方,并逐渐消失。