小码哥的IT人生

首页 > JS > jQuery

jQuery 事件 - select() 方法 概述

jQuery 2022-06-01 12:11:47小码哥的IT人生shichen

jQuery 事件 - select() 方法

实例

在文本域后添加文本,以显示出提示文本:

$("input").select(function(){
  $("input").after(" Text marked!");
});

完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("input").select(function(){
    $("input").after(" Text marked!");
  });
});
</script>
</head>
<body>
<input type="text" name="FirstName" value="Hello World" />
<p>请试着选取输入域中的文本,看看会发生什么。</p>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

定义和用法

当 textarea 或文本类型的 input 元素中的文本被选择时,会发生 select 事件。

select() 方法触发 select 事件,或规定当发生 select 事件时运行的函数。

触发 select 事件

语法

$(selector).select()

完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("input").select(function(){
    $("input").after(" Text marked!");
  });
  $("button").click(function(){
    $("input").select();
  });
});
</script>
</head>
<body>
<input type="text" name="FirstName" value="Hello World" />
<p>请试着选取输入域中的文本,看看会发生什么。</p>
<button>触发输入域中的 select 事件</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

将函数绑定到 select 事件

语法

$(selector).select(function)
参数 描述
function 可选。规定当发生 select 事件时运行的函数。

完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("input").select(function(){
    $("input").after(" Text marked!");
  });
});
</script>
</head>
<body>
<input type="text" name="FirstName" value="Hello World" />
<p>请试着选取输入域中的文本,看看会发生什么。</p>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024