当我们使用 window. getSection () 代码时, 如何在选择文本后显示一个小图像?
The most similar thing I found was this How can I position an element next to user text selection? but cant seem to get it to work properly :-s Being kinda new to this also doesn t help.
只要需要一些简单的代码来完成它, 图像在哪里出现并不重要, 只要接近选中的文本 。
- Edit -- - 爱迪特 -
正如我在评论中所说,我们的想法是显示一个在选定文本附近浮动的按钮(先考虑图像,但最好有一个按钮)(在选择后),并链接引用所选择的内容,如果我们澄清所选择的内容,则该按钮不再显示。
如果在完成文本选择和在要显示的按钮样式中添加 x, y coords 时拉动鼠标coords, 那么这是否可行?
- Edit -- - 爱迪特 -
找到这个 < a href=" http://motyar.blogspot.pt/2010/02/get-user-sective-sective- text- with-jquery-and.html" rel="no follown noreferrer" > http://motyar.blogspot.pt/2010/02/get-user-sective-text-wise-with-jquery-and.html ,我想到了这一点:
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;
}
$(document).ready(function() {
var blank = ,
selectionImage;
$( #mensagem ).mouseup(function(e) {
var selection = getSelected();
if (!selectionImage) {
selectionImage = $( <button> ).attr({
type: button ,
title: Citar Texto seleccionado ,
id: quote-place
}).html("Citar").css({
"color": "red"
}).hide();
$(document.body).append(selectionImage);
}
$("#quote-place").click(function quote() {
var txt = ;
if (window.getSelection) {
txt = window.getSelection();
}
else if (document.getSelection) {
txt = document.getSelection();
}
else if (document.selection) {
txt = document.selection.createRange().text;
}
else {
return;
}
document.aform.selectedtext.value = txt;
}).mousedown(function() {
if (selectionImage) {
selectionImage.fadeOut();
}
});
selectionImage.css({
top: e.pageY - 30,
//offsets
left: e.pageX - 13 //offsets
}).fadeIn();
});
});
http://jsfiddle.net/ordhor/2Gc8c/
当我们点击 lt; div>
而不选择该按钮继续显示的文本时,问题就在于现在。 它只应在选择文本时出现,而我找不到如何修正它...