使用 Javascript 到 trim 并发送到剪贴板
Using Javascript to trim and send to clipboard
我有下面的 Javascript 将 div 的内容复制到剪贴板。
div 的内容总是不同的,但是目前的结果总是有大约 5 个空行,每个空行由一个空白组成 space 在结果之前。我不能改变这个,所以我想要下面的函数 trim 结果前后的所有白色 space。
我知道
str.trim()
可能是最好的,但是作为一个javascript新手,我一直在努力将它插入下面。
<script>
function copyToClipboard(element) {
var $temp = $("<textarea>");
var brRegex = /<br\s*[\/]?>/gi;
$("body").append($temp);
$temp.val( $(element).html().replace(brRegex, "\r\n").replace(/<\/?[a-zA-Z]+\/?>/g, '')).select();
document.execCommand("copy");
$temp.remove();
}
有人能帮忙吗?
谢谢!
您的代码片段非常适合我。但是,如果你要在某处插入 trim,它会在这里:
function copyToClipboard(element) {
var $temp = $("<textarea>");
var brRegex = /<br\s*[\/]?>/gi;
$("body").append($temp);
$temp.val($(element).html().replace(brRegex, "\r\n").replace(/<\/?[a-zA-Z]+\/?>/g, '').trim()).select();
document.execCommand("copy");
$temp.remove();
}
解释是$(element).html()是一个字符串,是你要trim替换后的字符串您要替换的东西。
html 的完整代码片段:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Copy</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script
src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous"
></script>
</head>
<body>
<div class="copy" contenteditable="true">HI<br />I am me! <span>I want to kill you!</span></div>
<button class="copy-button">copy</button>
<script>
function copyToClipboard(element) {
var $temp = $("<textarea>");
var brRegex = /<br\s*[\/]?>/gi;
$("body").append($temp);
$temp.val($(element).html().replace(brRegex, "\r\n").replace(/<\/?[a-zA-Z]+\/?>/g, '').trim()).select();
document.execCommand("copy");
$temp.remove();
}
$('.copy-button').on('click', _ => {
copyToClipboard($('.copy'));
console.log("HI");
});
</script>
</body>
</html>
我有下面的 Javascript 将 div 的内容复制到剪贴板。
div 的内容总是不同的,但是目前的结果总是有大约 5 个空行,每个空行由一个空白组成 space 在结果之前。我不能改变这个,所以我想要下面的函数 trim 结果前后的所有白色 space。
我知道
str.trim()
可能是最好的,但是作为一个javascript新手,我一直在努力将它插入下面。
<script>
function copyToClipboard(element) {
var $temp = $("<textarea>");
var brRegex = /<br\s*[\/]?>/gi;
$("body").append($temp);
$temp.val( $(element).html().replace(brRegex, "\r\n").replace(/<\/?[a-zA-Z]+\/?>/g, '')).select();
document.execCommand("copy");
$temp.remove();
}
有人能帮忙吗?
谢谢!
您的代码片段非常适合我。但是,如果你要在某处插入 trim,它会在这里:
function copyToClipboard(element) {
var $temp = $("<textarea>");
var brRegex = /<br\s*[\/]?>/gi;
$("body").append($temp);
$temp.val($(element).html().replace(brRegex, "\r\n").replace(/<\/?[a-zA-Z]+\/?>/g, '').trim()).select();
document.execCommand("copy");
$temp.remove();
}
解释是$(element).html()是一个字符串,是你要trim替换后的字符串您要替换的东西。
html 的完整代码片段:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Copy</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script
src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous"
></script>
</head>
<body>
<div class="copy" contenteditable="true">HI<br />I am me! <span>I want to kill you!</span></div>
<button class="copy-button">copy</button>
<script>
function copyToClipboard(element) {
var $temp = $("<textarea>");
var brRegex = /<br\s*[\/]?>/gi;
$("body").append($temp);
$temp.val($(element).html().replace(brRegex, "\r\n").replace(/<\/?[a-zA-Z]+\/?>/g, '').trim()).select();
document.execCommand("copy");
$temp.remove();
}
$('.copy-button').on('click', _ => {
copyToClipboard($('.copy'));
console.log("HI");
});
</script>
</body>
</html>