8.文本选择window.getSelection 例 1.8(文本选择IEFF.html) <html> <head> <meta http-equiv="content-type" content ="text/html; charset=utf-8"/> <script type="text/javascript"> function fun1(){ if(window.getSelection window.getSelection is "+window.getSelection); selectionText = window.getSelection(); }else if(document.getSelection ){//IE10 selectionText = document.getSelection(); }else if(document.selection){//IE6+10- selectionText
DocumentOrShadowRoot.getSelection(), Selection.rangeCount, Selection.getRangeAt(), Selection.removeAllRanges document.getSelection().getRangeAt(0) // Store selection if found : false; (selected) { // If a selection existed before copying document.getSelection ().removeAllRanges(); // Unselect everything on the HTML document document.getSelection().addRange 思路 1、首先创建一个 targetNode,设置绝对布局,赢藏我们的元素 2、document.getSelection() 已经由 window.getSelection() 替代了,具体流程如上
().anchorNode.tagName; } else { console.log(window.getSelection()); thisTagName = window.getSelection = null) { if (thisTagName == element.tagName) { if (window.getSelection().anchorNode.textContent == $(element).text()) { result = window.getSelection().anchorOffset; } else { var currentIndex = window.getSelection ().anchorOffset; var txt = ""; var txtoo = window.getSelection().anchorNode.previousSibling; while ().anchorOffset; var txt = ""; var txtoo = window.getSelection().anchorNode.parentElement.previousSibling
<script>
document.body.addEventListener('copy', function (e) {
if (window.getSelection().toString () && window.getSelection().toString().length > 10) {
setClipboardText(e);
}
});
function >
'
+ window.getSelection().toString();
var textData = ''
+ '著作权归作者所有 >\n\n'
+ window.getSelection().toString();
clipboardData.setData('text/html', htmlData
自带版权说明代码document.body.addEventListener('copy', function (e) { if (window.getSelection().toString() && window.getSelection().toString().length > 10) { setClipboardText(e); }}); function setClipboardText >
' + window.getSelection().toString(); var textData = '' + '著作权归作者所有 >\n\n' + window.getSelection().toString(); clipboardData.setData('text/html', htmlData
document.body.addEventListener('copy', function (e) {
if (window.getSelection().toString() && window.getSelection >
'
+ window.getSelection().toString();
var textData = ''
+ '著作权归作者所有。 >\n\n'
+ window.getSelection().toString();
clipboardData.setData('text/html', htmlData);
clipboardData.setData
这里,另外还会使用到一个新的 API, window.getSelection()。具体来说就是: getSelection(): 用来获得当前选中的元素的内容。一般而言就是用鼠标选中页面上的内容。 基本使用就是: // 输出选中的文本 window.getSelection().toString(); 我们一般只是使用该 API 进行辅助作用。 当然,上面的 getSelection() 也是其中之一。用到的 API 有: document.createRange(): 用来创建选中容器。返回一个 range Object。 只能添加一个 window.getSelection() addRange(range): 这个方法是挂载到 getSelection() 方法下的,用来执行元素的选中。(! range = document.createRange(); // 选中需要复制的节点 range.selectNode(copyDOM); // 执行选中元素 window.getSelection
, _editor = $editor[0]; function surrounds(){ setTimeout(function () { //chrome var sel = window.getSelection && window.getSelection().getRangeAt){ range = window.getSelection().getRangeAt(0); range.insertNode (img); range.collapse(false); var sel = window.getSelection(); sel.removeAllRanges(); //删除 _editor.focus(); _editor.blur(); //输入表情时禁止输入法 setTimeout(function(){ range = window.getSelection ().getRangeAt(0); range.collapse(false); var sel = window.getSelection(); sel.removeAllRanges
footer.php 最下面放入以下代码就可以了
## 代码
document.body.addEventListener('copy', function (e) {
if (window.getSelection ().toString() && window.getSelection().toString().length > 42) {
setClipboardText(e);
notie window.location.href + '
'
+ '来源:https://www.52xzv.cn/
'
+ window.getSelection 链接:' + window.location.href + '\n'
+ '来源:https://www.52xzv.cn/\n\n'
+ window.getSelection
其中用户在谷歌、火狐浏览器中选择文本时需要借助getSelection()方法,而IE浏览器则借助的是selection()方法。 其具体的用法及区别一起来看吧~ 3.1 getSelection()方法 getSelection()方法返回的是一个selection对象,表示的是用户选择到的文本内容,支持谷歌、火狐、苹果等浏览器, 具体写法如下: if(window.getSelection) { text = window.getSelection(); } else { if(document.getSelection ) { text = document.getSelection(); } else { text = ""; } } 3.2 selection对象 selection
window.getSelection() : document.selection.createRange().text) ? layer.msg("请选择需要复制的内容!") : document.execCommand("Copy") } function baiduSearch() { var a = window.getSelection window.getSelection() : document.selection.createRange().text; "" == a ? wd=" + a) } function googleSearch() { var a = window.getSelection ? window.getSelection() : document.selection.createRange().text; "" == a ?
window.getSelection() : document.selection.createRange().text) ? layer.msg("请选择需要复制的内容!") : document.execCommand("Copy") } function baiduSearch() { var a = window.getSelection window.getSelection() : document.selection.createRange().text; "" == a ? wd=" + a) } function googleSearch() { var a = window.getSelection ? window.getSelection() : document.selection.createRange().text; "" == a ?
window.getSelection() : document.selection.createRange().text) ? layer.msg("请选择需要复制的内容!") : document.execCommand("Copy") } function baiduSearch() { var a = window.getSelection window.getSelection() : document.selection.createRange().text; "" == a ? wd=" + a) } function googleSearch() { var a = window.getSelection ? window.getSelection() : document.selection.createRange().text; "" == a ?
1 } } }) })(jQuery); function getSelect() { "" == (window.getSelection ? window.getSelection() : document.selection.createRange().text) ? layer.msg("请选择需要复制的内容!") : document.execCommand("Copy") } function baiduSearch() { var a = window.getSelection ? window.getSelection() : document.selection.createRange().text; "" == a ? layer.msg("请选择需要百度的内容!") wd=" + a) } function googleSearch() { var a = window.getSelection ?
文本选择 vx-hookhttps://www.npmjs.com/package/vx-hook vue 3.0 hook 库 getSelection 文本选择 Uesage <template return { ...initState } } // 获取选择文本及坐标 function getRectFromSelection(){ const selection = window.getSelection window.getSelection) return; const data = getRectFromSelection() if(data.text){ state.value window.getSelection) return; if(state.value.text){ state.value = getInitState() } window.getSelection().removeAllRanges() } /** * 文本选择 * @param { element } intiDom 绑定元素
当然如果考虑到用户体验的话建议只放在post.php文件即文章内页php文件
<script>
document.body.addEventListener('copy', function (e) {
if (window.getSelection ().toString() && window.getSelection().toString().length > 10) {
setClipboardText(e);
}
} >
'
+ window.getSelection().toString();
var textData = ''
+ '著作权归作者所有 >\n\n'
+ window.getSelection().toString();
clipboardData.setData('text/html', htmlData
不IE浏览器使用window.getSelection对象选择当前用户激活区域。 function(){ if(document.selection&&document.selection.empty){ document.selection.empty(); }else if (window.getSelection ) { var sel=window.getSelection(); sel.removeAllRanges(); } }; $("#element").bind("dblclick",function
这里我们会用到window.getSelection() api来获取光标所在的dom,以及光标在dom中文本的位置。 insertText代码修改如下 export function insertText(root, text, path) { const domSelection = window.getSelection 简单介绍一下setBaseAndExtent方法 // dom 是指要选中的dom节点,offset是指dom节点里面文字的位置 window.getSelection().setBaseAndExtent () setTxtOffset(anchorOffset + e.data.length) setTxt(getText) window.getSelection() dom.childNodes && dom.childNodes[0]) { dom = dom.childNodes[0] } window.getSelection
} } }) })(jQuery); function getSelect() { "" == (window.getSelection window.getSelection() : document.selection.createRange().text) ? layer.msg("啊噢...你没还没选择文字呢!") : document.execCommand("Copy") } function baiduSearch() { var a = window.getSelection window.getSelection() : document.selection.createRange().text; "" == a ?
所以这个的事件一定是 onmouseup ,盒子显示而且盒子的位置 在 鼠标的 clientX 和 clientY 一模一样 用来判断选择的文字 二、获得用户选择内容 window.getSelection () 标准浏览器 document.selection.createRange().text; ie 获得选择的文字 兼容性的写法: if(window.getSelection) { txt = window.getSelection().toString(); // 转换为字符串 } else { txt = document.selection.createRange event.clientX;//鼠标的x坐标 45 var y=event.clientY; 46 var txt;//存文字的变量 47 if(window.getSelection ){//获取选中的文字 48 txt=window.getSelection().toString();//转换为字符串 49 }else{ 50