Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何让复制文本在不同的div上工作?_Javascript - Fatal编程技术网

Javascript 如何让复制文本在不同的div上工作?

Javascript 如何让复制文本在不同的div上工作?,javascript,Javascript,我有一个“div”可以复制文本,我可以将它粘贴到其他地方 <div id="div1"> <div> Positive Comments: <br/> <textarea id="txtArea" rows="3" name="txtCommentPositive#CurrentRow#"> #reReplaceCommentpositive </

我有一个“div”可以复制文本,我可以将它粘贴到其他地方

<div id="div1">
    <div>
        Positive Comments:
        <br/>
        <textarea id="txtArea" rows="3" name="txtCommentPositive#CurrentRow#">
            #reReplaceCommentpositive
        </textarea>
    </div>
    <div>
        Negative Comments:
        <br/>
        <textarea rows="3" name="txtCommentNegative#CurrentRow#">
             #reReplaceCommentnegative#
        </textarea>
    </div>
    <input type="button" id="btn" value="copy" onclick="copyText()" />
</div>

在无数次重读您的问题后,它突然点击:似乎您想将
div1
中的代码作为一种可重复使用的“模式”。
但是,您的javascript当前只有一个div-id硬编码

我不知道如何处理
div2
(从您的函数中),因此我将其(例如)更改为
textarea
(具有
id
clipboardText
),它将显示刚刚复制到剪贴板的文本内容

为简单起见(与您当前的代码逻辑/样式保持一致),我建议该按钮是div的直接子级,它需要从中获取
innerText
。如果是这种情况(并且我们继续使用内联脚本),您应该将
(指单击的按钮)传递给函数。在函数中,您只需获取按钮的父节点的
内部文本

Javascript:

function copyText() {
    var copyDivText = document.getElementById('div1').innerText;
    var returnVal = window.clipboardData.setData('Text', copyDivText);
    alert(returnVal);

    document.getElementById('div2').innerText = window.clipboardData.getData('Text');
}
function copyText(t) {
    var copyDivText = t.parentNode.innerText;
    var returnVal = window.clipboardData.setData('Text', copyDivText);
    alert(returnVal);

    document.getElementById('clipboardText').value = window.clipboardData.getData('Text');
}
HTML正文(注意,我添加了另一个id为
div2
的“模式”,因为它似乎与我猜您正在做的事情是一致的):



正面评论:
#重新定位 负面评论:
#重新替换为负# 正面评论:
#重新定位 负面评论:
#重新替换为负#
希望这就是你想问的问题


PS:如果这是你想要的,注意很多人认为内联脚本不好,当你动态地附加你的函数时,你就不必通过<代码>这个< /代码>了(此后,代码>这个< /代码>可以直接在你的函数中调用)。你为什么用它来代替一个普通变量(出于兴趣而问)?同样,

innerText
。你能创建一个提琴吗?@gitaarlab是的,我想创建,我只想在IE中使用,不需要其他浏览器
<textarea id="clipboardText"></textarea>
<hr>
<div id="div1">
    <div>
        Positive Comments:
        <br/>
        <textarea id="txtArea" rows="3" name="txtCommentPositive#CurrentRow#">
            #reReplaceCommentpositive
        </textarea>
    </div>
    <div>
        Negative Comments:
        <br/>
        <textarea rows="3" name="txtCommentNegative#CurrentRow#">
             #reReplaceCommentnegative#
        </textarea>
    </div>
    <input type="button" id="btn" value="copy" onclick="copyText(this);" />
</div>
<div id="div2">
    <div>
        Positive Comments:
        <br/>
        <textarea id="txtArea" rows="3" name="txtCommentPositive#CurrentRow#">
            #reReplaceCommentpositive
        </textarea>
    </div>
    <div>
        Negative Comments:
        <br/>
        <textarea rows="3" name="txtCommentNegative#CurrentRow#">
             #reReplaceCommentnegative#
        </textarea>
    </div>
    <input type="button" id="btn" value="copy" onclick="copyText(this);" />
</div>