我有一个 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/