javascript - 我怎么称呼选择事件?选择事件在 jquery 中不起作用
问题描述
这是我的代码,当我调用 onselect 事件时它不起作用。当我在输入标签中调用它时,我如何调用它,当我在输入标签中调用它时,我需要在选择 peragraph 文本时调用它。谢谢
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
p {
color: blue;
}
div {
color: red;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<div>
<h1>Attach tooltips to text selection</h1>
<p>
You can also try to select a text, then select something else, and see the tooltip automatically update to being properly positioned.<br />
If you scroll this text while the tooltip is visible, you'll notice how it will automatically flip to stay between the viewport.
</p>
<div></div>
<hr />
</div>
<script>
$( "p" ).select(function() {
alert('hye');
});
</script>
</body>
</html>
解决方案
没有select
事件p
。你可以利用mouseup
事件。
例子。
$(function () {
$("p").bind('mouseup', function (e) {
var selection;
if (window.getSelection) {
selection = window.getSelection();
} else if (document.selection) {
selection = document.selection.createRange();
}
selection.toString() !== '' && alert('"' + selection.toString() + '" was selected at ' + e.pageX + '/' + e.pageY);
});
});
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<div>
<h1>Attach tooltips to text selection</h1>
<p>
You can also try to select a text, then select something else, and see the tooltip automatically update to being
properly positioned.<br />
If you scroll this text while the tooltip is visible, you'll notice how it will automatically flip to stay between
the viewport.
</p>
<div></div>
<hr />
</div>
推荐阅读
- java - API 26 中的服务
- c# - .NET Core 中的 AppDomainAppId?
- javascript - 如何使用 React Native 调用没有本机模块的本机代码
- python - 我不明白调试解释了什么
- javascript - 在 javascript 中的对象文字内的 IIFE 中使用“this”的问题
- python - 如何将 discord.py remove_roles 用于多个角色?(作为参数的对象列表)
- javascript - ES6 使用 " vs. '
- oracle - 特定日期范围内的汇总日期
- angular - 如何在 Angular 应用程序中构建模型和服务?
- php - 在 Woocommerce 中显示四舍五入的两位小数价格