Javascript 是否在contenteditable div中突出显示的文本上方显示弹出窗口?
我正在我的一个应用程序中构建一个简单的博客风格的功能,并使用HTML5 contenteditable属性,因为它是干净的。但是,我现在需要做的是,当用户突出显示contenteditable div中的某些文本时,需要在其上方显示一个弹出窗口。现在,我有一个函数来获取所选文本,该文本绑定到DIV的mouseup()。但是,当我点击contenteditable div时,该函数被触发 这是我的密码:Javascript 是否在contenteditable div中突出显示的文本上方显示弹出窗口?,javascript,jquery,css,html,Javascript,Jquery,Css,Html,我正在我的一个应用程序中构建一个简单的博客风格的功能,并使用HTML5 contenteditable属性,因为它是干净的。但是,我现在需要做的是,当用户突出显示contenteditable div中的某些文本时,需要在其上方显示一个弹出窗口。现在,我有一个函数来获取所选文本,该文本绑定到DIV的mouseup()。但是,当我点击contenteditable div时,该函数被触发 这是我的密码: function getSelected() { if (window
function getSelected() {
if (window.getSelection) {
return window.getSelection();
}
else if (document.getSelection) {
return document.getSelection();
}
else {
var selection = document.selection && document.selection.createRange();
if (selection.text) {
return selection.text;
}
return false;
}
return false;
};
$("#content-create-partial").bind("mouseup", function(){
var text = getSelected();
if(text) {
console.log(text);
}
else{
console.log("Nothing selected?");
};
});
如何防止在用户单击contenteditable div时触发调用,并且仅当用户突出显示某些文本时才触发调用?将其转换为字符串(.toString()
),如果所选字符串为空,请使用trim()
,并在mouseup
事件中检查所选字符串的长度
$("#content-create-partial").bind("mouseup", function(){
if (document.getSelection) {
var text = getSelected();
if(text) {
console.log(text);
}
else{
console.log("Nothing selected?");
}
}
});
$("#content-create-partial").bind("mouseup", function(){
var text = getSelected().toString().trim();
if(text.length>0) {
console.log(text);
}
else{
console.log("Nothing selected?");
};
});
那么,您的代码有什么不正确的地方呢?