【发布时间】:2019-10-08 13:00:01
【问题描述】:
如果我同时选择两行并使用window.getSelection().toString() 获取文本,那么我会在两个单词之间得到↵(上一行的最后一个单词和下一行的第一个单词)。以下是我的代码-
document.addEventListener('dblclick',function (event) {
var element = document.getElementsByClassName(event.target.parentElement.parentElement.parentElement.className);
var T= window.open("", "MsgWindow", "width=200,height=500");
T.document.write(element[0].innerText.replace(/\n/g, "<br/>"));
T.document.write(`<script>
window.addEventListener("keydown", function(e){
if(e.keyCode === 16) {
var text = "";
if (window.getSelection) {
text_1 = window.getSelection().toString();
text = text_1.replace(/\u21b5/g," "); // my attempt to corrrect
}
words = text.split(" ");
console.log(words);
}
},false);
</script>`);
},false);
例如,如果我选择 -
侧面
部分
然后按shift键我得到-
Array(1)
0: "Sides↵Section"
length: 1
__proto__: Array(0)
但输出应该是-
Array(1)
0: "Sides Section"
length: 1
__proto__: Array(0)
如何将↵ 替换为空格?
PS:我根据SE上给出的解决方案尝试了一些可能性,但没有奏效,请不要认为这是微不足道的或重复的帖子。
【问题讨论】:
-
我试过了,但没有得到我想要的结果
-
换行符就是这样显示的,有很多方法可以去掉。试试
variable.replace("\n", " ") -
@GammaGames 我试过了,你自己检查一下,还是不行,不知道是什么原因!
-
@Mike 人们试图提供帮助,而您的 cmets 可能会被视为粗鲁。
标签: javascript replace