标签:false document int pos menu 事件 右键 select use
代码:
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();
})
选取文本自动显示菜单条
标签:false document int pos menu 事件 右键 select use
原文地址:http://www.cnblogs.com/Marys/p/6525011.html