2 回答
TA贡献1841条经验 获得超3个赞
不要使用,因为它只支持单行文本,并将删除换行符
<input>
用于添加新行
\n
添加到以呈现换行符
white-space:pre
#thecopiedtext
$("#copystuff").click(function() {
var temp = $("<textarea>");
$("body").append(temp);
var previewHeader = $("#PreviewHeader").text();
var HiddenURLdiv = $("#HiddenURLdiv").text();
var contentTogether = previewHeader + "\n" + HiddenURLdiv;
temp.val(contentTogether).select();
document.execCommand("copy");
$("#thecopiedtext").text(contentTogether);
temp.remove();
});
#HiddenURLdiv {
display: none;
}
#thecopiedtext {
white-space: pre;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="PreviewHeader">Hello stuff is here</div>
<div id="HiddenURLdiv">This one is hidden</div>
<a href="#" id="copystuff">Copy Stuff</a>
<div id="thecopiedtext"></div>
TA贡献1776条经验 获得超12个赞
如注释中所述,您可以将两者联系,另外,您应该使用或者您可以将两个元素包装在(或任何显示块标签,如)标签中,因为默认情况下具有:<br />.html()<div><p><div>display:block
$("#copystuff").click(function() {
var temp = $("<input>");
$("body").append(temp);
var previewHeader = $("#PreviewHeader").text();
var HiddenURLdiv = $("#HiddenURLdiv").text();
var contentTogether = `<div>${previewHeader}</div><div>${HiddenURLdiv}</div>`;
temp.val(`${previewHeader}${HiddenURLdiv}`).select();
document.execCommand("copy");
$("#thecopiedtext").html(contentTogether);
temp.remove();
});
#HiddenURLdiv {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="PreviewHeader">Hello stuff is here</div>
<div id="HiddenURLdiv">This one is hidden</div>
<a href="#" id="copystuff">Copy Stuff</a>
<div id="thecopiedtext"></div>
添加回答
举报