首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用鼠标选择文本和更改选定的颜色?

如何使用鼠标选择文本和更改选定的颜色?
EN

Stack Overflow用户
提问于 2020-01-14 06:47:22
回答 2查看 92关注 0票数 0

代码语言:javascript
复制
function getSelectedText() {
  var selectedText = '';
  if (window.getSelection) {
    selectedText = window.getSelection();
  } else if (document.getSelection) {
    selectedText = document.getSelection();
  } else if (document.selection) {
    selectedText =
      document.selection.createRange().text;
  } else return;
  document.testform.selectedtext.value = selectedText;
}
代码语言:javascript
复制
<html>

<head>
  <title>Selected Text</title>
</head>

<body>
  <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
    survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
    software like Aldus PageMaker including versions of Lorem Ipsum.
  </p>
  <p>
    <b>Why do we use it?</b>
    <br/> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content
    here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
    versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
  </p>
  <input type="button" value="Get Selection" onmousedown="getSelectedText()">
  <form name="testform">
    <textarea name="selectedtext" rows="5" cols="20"></textarea>
  </form>
</body>

</html>

在这段代码中,我通过鼠标选择文本,但只有当我单击get selection按钮时才能选择文本。我想通过鼠标选择文本,并更改所选文本的背景色,它将显示仍然被选中。那我该怎么做呢?请帮帮我。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-14 07:05:49

您可以使用mouseupmousedown模拟drag文本,也可以用span包装选定的文本以更改颜色。

代码语言:javascript
复制
function getSelectedText() {
  var selection = window.getSelection().getRangeAt(0);
  var selectedText = selection.extractContents();
  var span = $("<span class='selected'>" + selectedText.textContent + "</span>");
  selection.insertNode(span[0]);

  if (selection) {
    $('textarea').text(selection)
  }

}

$('p').mouseup(function() {
  getSelectedText()
});

$('p').mousedown(function() {
  $(this).find('.selected').contents().unwrap();;
});
代码语言:javascript
复制
.selected {
  background: red;
  color: white;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
  software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
  <b>Why do we use it?</b>
  <br/> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content
  here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
  versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
<form name="testform">
  <textarea name="selectedtext" rows="5" cols="20"></textarea>
</form>

编辑:改进的jQuery版本替换

票数 3
EN

Stack Overflow用户

发布于 2020-01-14 09:00:53

我们有Document通过打开和关闭来控制整个可编辑的文档。document.designMode = "on“{##**$$}} "off”

文档可编辑后,需要执行更改字体颜色的命令。

引用其他命令:https://developer.mozilla.org/enUS/docs/Web/API/Document/execCommand#Commands

document.execCommand("ForeColor",false,"red");

您可以使用鼠标事件来执行您的函数。

代码语言:javascript
复制
function getSelectedText() { 
            var selectedText = '';
            if (window.getSelection) { 
                selectedText = window.getSelection(); 
            }
            else if (document.getSelection) { 
                selectedText = document.getSelection(); 
            }  
            else if (document.selection) { 
                selectedText = 
                document.selection.createRange().text; 
            } 
            else return;
            document.testform.selectedtext.value = selectedText; 
			
			  document.designMode = "on";
			  document.execCommand("ForeColor", false, "red");
			  document.designMode = "off";
        } 
代码语言:javascript
复制
<p>
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
    <p>
        <b>Why do we use it?</b>
        <br/>
        It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    </p>
    <input type="button" value="Get Selection" onmousedown="getSelectedText(event)">
    <form name="testform"> 
        <textarea name="selectedtext" rows="5" cols="20"></textarea> 
    </form> 

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59728733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档