简体   繁体   English

使用DIV作为按钮时保持文本选择

[英]Maintain Text Selection When Using DIV as a button

Maybe this is a browser related issue? 也许这是与浏览器相关的问题? I'm on Chrome at the moment... 我现在在Chrome上...

What I'm trying to accomplish is allow the user to save text that they've selected (highlighted) on the page with their cursor. 我想要完成的是允许用户使用光标保存他们在页面上选择(突出显示)的文本。

I've got the javascript/jQuery/ajax all setup...and it works like a charm when I'm testing it via the console. 我有javascript / jQuery / ajax所有设置......当我通过控制台测试它时它就像一个魅力。

ie - I select some text and then run this command. 即 - 我选择一些文本,然后运行此命令。

alert(getSelected());

To which I get an alert popup with the selected characters. 我得到一个包含所选字符的警报弹出窗口。 ...so it works in theory. ......所以它在理论上有效。

HOWEVER - When I want to use a button to make the call, what's happening is that the text selection dissapears FIRST, and thus I am unable to then see/save what they've selected. 但是 - 当我想使用按钮拨打电话时,发生的事情是文本选择消失了FIRST,因此我无法查看/保存他们选择的内容。

<script>
$('#save').click(function(){
  var selected_text = getSelected();
});
</script>

<div id="save"><img src="the_save_button.jpg" height="50" width="50" /></div>

Is there a way to maintain the text selection upon clicking/after clicking the "save" DIV? 有没有办法在点击/点击“保存”DIV后保持文本选择?

Open to any and all solutions!! 开放给任何和所有解决方案! Thanks! 谢谢!

One easy option is to use mousedown instead of click . 一个简单的选择是使用mousedown而不是click However, this is different to the native behaviour of buttons, which is to fire an action when the mouse button is released, so is not great UI. 但是,这与按钮的本机行为不同,即在释放鼠标按钮时触发动作,因此UI不是很好。

Another option is to make the clickable element unselectable, using a combination of proprietary CSS properties and the unselectable attribute for IE and Opera, which prevents the click from clearing the selection: 另一种选择是使可点击元素不可选,使用专有CSS性质的组合和unselectable用于IE和Opera属性,这防止了从点击清除选择:

Demo: http://jsfiddle.net/timdown/UUQJs/ 演示: http//jsfiddle.net/timdown/UUQJs/

CSS: CSS:

.unselectable {
    -khtml-user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

HTML: HTML:

<div id="save" unselectable="on" class="unselectable"><img
   src="the_save_button.jpg" height="50" width="50" unselectable="on" /></div>

Try the folowing: 尝试下面的内容:

$('#save').click(function(){
   var selected_text = "";
   if (window.getSelection) {
        selected_text = window.getSelection();
   } else if (document.getSelection) {
        selected_text = document.getSelection();
   } else if (document.selection) {
        selected_text = document.selection.createRange().text; 
   }
   alert(selected_text)
});

DEMO DEMO

or: 要么:

$('#save').mousedown(function(){
   // ...
});

DEMO DEMO

When the user selects text, store it in a global variable. 当用户选择文本时,将其存储在全局变量中。 Make sure you don't erase it when they deselect; 确保在取消选择时不要将其删除; the variable should hold the last text the user selected. 变量应该包含用户选择的最后一个文本。 Then the button can reference this text. 然后按钮可以引用此文本。 You can probably also use something like this: 你也可以使用这样的东西:

$('html').click(function() {
    global_var = '';
});
$('the button').click(function(event) {
    event.stopPropagation();
});

To allow the text to be removed except when the user clicks on the button. 除非用户单击按钮,否则允许删除文本。

As soon as the selection have been made, you should assign it to a variable and save it and then only use the button click event for ajax queries. 选择完成后,应将其分配给变量并保存,然后仅使用按钮单击事件进行ajax查询。 The problem involved here is that as soon as the click event is initiated, you are deselecting it again(You will notice the blue background selection part is gone once the mouse is cliked) and hence it returns nothing. 这里涉及的问题是,一旦启动了click事件,你就会再次取消选择它(你会注意到一旦鼠标被阻塞,蓝色背景选择部分就会消失),因此它什么都不返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM