0

我有某些将<div>内容复制到<textarea>的代码,但它不会复制<div>. 它以普通字体粘贴。我该如何解决这个问题?

HTML

<input type="text" id="styledText" placeholder="Place your Name..." size="50" name="styledText" class="changeMe">
<div id="fontselect" class="changeMe" onClick="copyText()" style="font-family: Times New Roman;font-size: 22px; font-weight: bold; cursor:pointer;">Place your Name...</div>

Javascript

function copyText() {
    var output = document.getElementById("fontselect").innerHTML;
    document.getElementById("styledText").value = output;
}

提前致谢 :)

4

2 回答 2

2

请记住,这font-family将适用于所有插入的文本,因此任何单独样式的单词/内容都将失去它们的区别:

function copyText() {
    var output = document.getElementById("fontselect"),
        textElem = document.getElementById("styledText");
    textElem.value = output.value;
    textElem.style.fontFamily = window.getComputedStyle(output,null).fontFamily || output.style.fontFamily || output.currentStyle.getCurrentProperty('font-family');

}
于 2012-09-04T15:09:52.413 回答
0
#fontselect, #styledText {
    font-family: Times New Roman;
    font-size: 22px;
    font-weight: bold;
    cursor:pointer;
}
于 2012-09-04T15:11:48.347 回答