javascript - 检查选定的文本是否被点击
问题描述
我有一个 div 元素,里面有文本。我想知道用户点击了什么,选择了文本还是没有选择?区分选中文本点击和未选中文本点击的主要目标
解决方案
这将运行 2 个不同的功能。当你点击选中的文本时,它会运行一个函数,使背景变红,当你点击未选中的文本时,它会运行另一个函数,使背景变绿。希望这有帮助。
function red(){
//this function will be executed when the selected text is clicked
document.body.style.backgroundColor = "red";
}
function green(){
/*this function will be executed when the non-selected text is clicked*/
document.body.style.backgroundColor = "green";
}
body{
font-size:30px;
}
<body>
<div><span onclick="red()" style="background-color:yellow">this text is selected</span>,<span onclick="green()"> this text is not</span></div>
</body>
推荐阅读
- android - RuntimeException : 在应用购买中调用 BillingClient 函数时
- javascript - Angular - 从外部 JS 库(Bambora 支付网关)调用函数
- iis - 已提交的内存(提交限制)未反映
- playwright - playwright._impl._api_types.Error: Host system is missing dependencies??
- azure - 带有容器的 Azure AppService (Windows) 仅在 FTP 上显示 hostingstart.html
- php - SQL 获取已收到发送消息的用户(UPDATE)
- r - ggplot:当纵横比!= 1时,保持圆圈环视
- python - Jupyterhub:Authenticator.pre_spawn_start 中的错误
- laravel - orderBy created_at 上的多对多关系 laravel 和 eloquent
- javascript - 防止用户在移动设备上滚动 div