javascript - 检查何时选择/标记了一个元素

标签 javascript jquery html

我有一个 HTML 页面,其中有一些带有 contenteditable="true"div。我还按顺序设置了 tabindex,这样当用户点击选项卡时,他或她将浏览所有可编辑的 div。但是,现在我在每个 div 中都写了“Insert notes here”(见下文)。

<div contenteditable="true" tabindex='1'>Insert notes here<div>
<div contenteditable="true" tabindex='2'>Insert notes here<div>
<div contenteditable="true" tabindex='3'>Insert notes here<div>

当用户切换到 div 时,我试图摆脱“Insert notes here”文本。现在,如果他们使用以下 jQuery 单击文本,我就可以删除文本:

function selectText(containerid) {
   if(document.getElementById(containerid).innerHTML == "Insert notes here") {
      document.getElementById(containerid).innerHTML = "";
   }
}

有没有办法在用户使用 tab 时也能达到同样的效果?

最佳答案

听起来你正在寻找 onfocus 事件

http://www.w3schools.com/jsref/event_onfocus.asp

在 jquery 中:

$("#fieldId").focus(function() {
    //your code here 
});

关于javascript - 检查何时选择/标记了一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38533492/

相关文章:

jquery - IE 支持空白 :nowrap

javascript - 如何检查表单元素(输入或按钮等)是否被其本身或 Javascript 中的父字段集禁用?

javascript - 如何使用 Google Apps 脚本加粗特定文本?

javascript - 多选列表框不工作

javascript - 如何将字符串转换为对象

javascript - 为什么我没有得到返回值javascript

javascript - 我可以使用 javascript 来更改语言吗?

jQuery .mousemove() 与 :hover

javascript - 单击时如何将可拖动的 div 置于最前面?

html - 如何在 Bootstrap 中将左侧边栏置于移动 View 内容下方?