我想復制具有 CSS 類的文本,copytext
并希望粘貼到具有 CSS 類的同一頁面上pastehere。
我正在使用以下代碼,但沒有結果。
function copyToClipboard(text) {
const elem = document.createElement('.copytext');
elem.value = text;
document.body.appendChild(elem);
elem.select();
document.execCommand('.pastehere');
document.body.removeChild(elem);
}
uj5u.com熱心網友回復:
我認為您想獲得 HTML 的價值
標簽什么的?使用 DOM Element innerHTML 你可以獲得標簽的值。例如:
html:
`<p id="myTag">hello world</p>`
javascript:
`let var1 = document.getElementById("myTag").innerHTML;`
“hello world”現在存盤在變數“var1”中,您可以繼續正常使用它
uj5u.com熱心網友回復:
您可以使用
document.getElementById("ID NAME")
復印。
要更改,您可以使用
document.getElementsByClassName("class-name").style[0] = "max-width: 10%;"
uj5u.com熱心網友回復:
- 如果要將文本從所有元素復制
.copytext到元素.pastehere:
HTML:
<div class="copytext">Line 1</div>
<div class="copytext">Line 2</div>
<div class="copytext">Line 3</div>
JS:
function fun(text){
let copiedText = "";
text.forEach(element => {
copiedText = element.textContent "\n";
});
const resault = document.createElement('div');
resault.classList.add('pastehere');
resault.innerText = copiedText;
document.body.appendChild(resault);
}
let copyFrom = document.querySelectorAll('.copytext');
fun(copyFrom);
你會得到:
<div class="pastehere">
Line 1 <br>
Line 2 <br>
Line 3 <br>
</div>
- 如果要使用作為引數傳遞給函式的文本創建元素:
JS:
function fun(text){
let resault = document.createElement('div');
resault.classList.add('pastehere');
resault.innerText = text;
document.body.appendChild(resault);
}
let text = "My text";
fun(text);
你會得到:
<div class="pastehere">
My text
</div>
!!請注意,您只能在document.createElement('');
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/490614.html
標籤:javascript html 复制 粘贴
上一篇:在分頁期間顯示元素數量
下一篇:相關下拉串列在編輯時不顯示值?
