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;
}<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按钮时才能选择文本。我想通过鼠标选择文本,并更改所选文本的背景色,它将显示仍然被选中。那我该怎么做呢?请帮帮我。
谢谢
发布于 2020-01-14 07:05:49
您可以使用mouseup和mousedown模拟drag文本,也可以用span包装选定的文本以更改颜色。
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();;
});.selected {
background: red;
color: white;
}<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版本替换
发布于 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");
您可以使用鼠标事件来执行您的函数。
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";
} <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>
https://stackoverflow.com/questions/59728733
复制相似问题