Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Javascript设置CSS3::selection_Javascript_Css_Google Chrome_Selection - Fatal编程技术网

如何使用Javascript设置CSS3::selection

如何使用Javascript设置CSS3::selection,javascript,css,google-chrome,selection,Javascript,Css,Google Chrome,Selection,是否有人知道如何使用Javascript动态执行此CSS规则,然后禁用它,特别是在某些鼠标手势期间抑制Chrome中令人讨厌的选择: ::selection{ background:transparent; } 例如:document.body.style['::selection']['background']=透明;,但这当然行不通 我能想到的一些方法: JS CSS 我尝试了这个解决方法,它有效: var styleElem=document.createElement('sty

是否有人知道如何使用Javascript动态执行此CSS规则,然后禁用它,特别是在某些鼠标手势期间抑制Chrome中令人讨厌的选择:

::selection{
    background:transparent;
}

例如:document.body.style['::selection']['background']=透明;,但这当然行不通

我能想到的一些方法:

JS

CSS


我尝试了这个解决方法,它有效:

var styleElem=document.createElement('style'); 
styleElem.innerHTML='::selection{ background:transparent; }';

// enable in some cases
document.head.appendChild(styleElem);

// disable when not needed
window.getSelection().removeAllRanges();
document.head.removeChild(styleElem);
已尝试selectstart事件,Chrome 39中仍会出现恼人的选择。使用document.body.style.pointerEvents='none';我的页面对任何鼠标事件都没有反应。无论如何,谢谢你。似乎::selection工作得很好,但我需要找到一种方法来禁用它,以便在某些情况下允许选择。
pointer-events: none;
var styleElem=document.createElement('style'); 
styleElem.innerHTML='::selection{ background:transparent; }';

// enable in some cases
document.head.appendChild(styleElem);

// disable when not needed
window.getSelection().removeAllRanges();
document.head.removeChild(styleElem);