以任何方式防止“取消选择”的高亮文本? [英] Any way to prevent "deselection" of highlighted text?

查看:103
本文介绍了以任何方式防止“取消选择”的高亮文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

突出显示此网页上的某些文字,然后基本上在文档的任何位置点击。



当用户通过CSS或Javascript点击特定元素时,是否有办法防止此行为?



例如:

  var element = document.getElementById(foo); 
foo.onclick = function(e){
//这里有一些魔法阻止取消选择发生
}

  foo.style.preventDeselect =true 

编辑:也许我可以存储选择,然后mouseclick恢复选择?有没有办法存储选择,然后重新选择



谢谢! onmousedown在FF和Safari中工作,但不是在IE中,$ b

解决方案

return false以及e.preventDefault对我有用[0]丢个板砖[0]引用|举报(0)不是方法):

  //点击'test'元素不会取消选择文本。 
var test = document.getElementById(test);
test.onmousedown = function(e){
e = e || window.event;
e.preventDefault();
}



如果可能,我仍然希望在IE中无错误



感谢Paolo Bergantino的onmousedown提示。


Highlight some text on this webpage, then click basically anywhere on the document. Your selection will disappear.

Is there a way to prevent this behavior when the user clicks on a specific element, either by CSS or Javascript?

E.g.:

var element = document.getElementById("foo");
foo.onclick = function(e){
   //some magic here that prevents deselection from occuring
}

or

foo.style.preventDeselect = "true";

Edit: Perhaps I could store the selection, then after "mouseclick" restore the selection? Is there a way to store aselection, and then reselect it?

Thanks!

解决方案

"return false" as well as "e.preventDefault()" in onmousedown works in FF and Safari, but not IE. The only solution for IE, as far as I can tell, is to throw an error.

This works in all browsers (but causes an error in IE, since preventDefault is not a method):

//clicking the 'test' element will not deselect text.
var test = document.getElementById("test");
test.onmousedown = function(e){
  e = e || window.event;
  e.preventDefault();
}

I'd still like to do this error-free in IE, if possible

Thanks to Paolo Bergantino for the the "onmousedown" tip.

这篇关于以任何方式防止“取消选择”的高亮文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆