1.span标签内容的复制粘贴
<span id="biao1" class="c3 ins">${loginSession}</span>
<input type="button" value="复制" id="copyBut" onclick="cp(document.getElementById('biao1'));" class="l30 w100 f12 cur cf bg-red bnone tc ibk ml25" />
<script>
function selectText(x) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(x);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = document.createRange();
selection.removeAllRanges();
range.selectNodeContents(x);
selection.addRange(range);
}
}
function cp(x)
{
selectText(x);
document.execCommand("copy");
alert('复制成功')
}
</script>
<input type="text" id="copy" value="我是文本框内容">
<button onclick="copy_click">复制</button>
<script>
function copy_click() {
var url = document.getElementById('copy');
url.select();
document.execCommand("copy");
alert('复制成功!')
}
</script>
3.点击标签内容进行复制
<span id="thisDiv" onclick="execClick" oncopy="execCopy(event);">我是span标签</span>
<script>
function execClick() {
document.execCommand("copy");
},
function execCopy(event) {
var thisDiv = document.querySelector('#thisDiv')
if (this.isIE()) {
if (window.clipboardData) {
window.clipboardData.setData("Text", thisDiv.textContent);
alert=('复制成功');
}
} else {
event.preventDefault();
if (event.clipboardData) {
event.clipboardData.setData("text/plain", thisDiv.textContent);
alert( '复制成功');
}
}
},
function isIE() {
var input = window.document.createElement("input");
if (window.ActiveXObject === undefined) return null;
if (!window.XMLHttpRequest) return 6;
if (!window.document.querySelector) return 7;
if (!window.document.addEventListener) return 8;
if (!window.atob) return 9;
if (!input.dataset) return 10;
return 11;
},
</script>