首页 > 代码库 > 选取文本自动显示菜单条
选取文本自动显示菜单条
代码:
function getSel(e) {
var e = e ? e : window.event;
if (window.getSelection) {
if (window.getSelection() == ‘‘) {
return false;
} else {
txt = window.getSelection().toString();
var div = document.getElementById(‘baike-menu‘);
div.style.display = ‘block‘;
var x = getMousePos(e).x + ‘px‘;
var y = (parseInt(getMousePos(e).y) - 25) + ‘px‘;
div.style.left = x;
div.style.top = y;
}
} else if (document.getSelection) {
if (document.getSelection() == ‘‘) {
return false;
} else {
txt = document.getSelection().toString();
var div = document.getElementById(‘baike-menu‘);
div.style.display = ‘block‘;
var x = getMousePos(e).x + ‘px‘;
var y = (parseInt(getMousePos(e).y) - 25) + ‘px‘;
div.style.left = x;
div.style.top = y;
}
} else if (document.selection) {
if (document.selection.createRange().text == ‘‘) {
return false;
} else {
txt = document.selection.createRange().text;
var div = document.getElementById(‘baike-menu‘);
div.style.display = ‘block‘;
var x = getMousePos(e).x + ‘px‘;
var y = (parseInt(getMousePos(e).y) - 25) + ‘px‘;
div.style.left = x;
div.style.top = y;
}
}
console.log(txt)
}
function getMousePos(event) {
var evt = event ? event : window.event;
var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
var x = evt.pageX || evt.clientX + scrollX;
var y = evt.pageY || evt.clientY + scrollY;
return {
‘x‘: x,
‘y‘: y
};
}
$(".l-case-content .content").mouseup(function(e) {
getSel(e);
})
//监听鼠标右键事件
document.oncontextmenu = function() {
$("#baike-menu").hide();
}
$("#baike-menu").mousemove(function() {
$("#baike-menu").show();
})
$("#baike-menu").mouseout(function() {
$("#baike-menu").hide();
})
选取文本自动显示菜单条
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。