gpt4 book ai didi

javascript - 将内容粘贴到 DIV 中

转载 作者:行者123 更新时间:2023-11-29 15:41:13 31 4
gpt4 key购买 nike

将文本或图像粘贴到 DIV(如输入)的方法是单击该 DIV 并执行粘贴注释,通常是 CTRL + V。

现在,我的问题是,即使未选择 DIV,是否可以使用 Javascript 将文本或图像粘贴到 DIV,甚至是 DIV(或输入)?

更新:

<div contenteditable="true" id="pasteCapture" style="height: 100px; width: 100px; " onpaste='handlepaste(this, event)'></div> 
<script>
function getfocus(){
document.getElementById('pasteCapture').focus()
}
</script>

到目前为止,这就是我所做的。选择 div 并进行粘贴时,没问题。但是,我的要求是这个 DIV 应该得到粘贴项,即使它没有在页面上被选中。

这样做的原因是我要隐藏这个 div。

最佳答案

如果我理解正确,您想拦截粘贴事件并将粘贴重定向到隐藏的 div 中。我可以做一些:

HTML

Paste Stuff Here: <input id='pasteCapture' type='text'></input>
<div contenteditable="true" id="pasteRedirect" style="height: 100px; width: 100px; " >
</div>
<button
onclick=" document.getElementById('pasteRedirect').style.display = 'block';"
>Show Div</button>

脚本

function handlePaste(e) {
document.getElementById('pasteRedirect').style.display = 'block';
document.getElementById('pasteRedirect').focus();
e.srcElement = document.getElementById("pasteCapture");
setTimeout( function() {
document.getElementById('pasteRedirect').style.display = 'none';
}, 0 );
}

document.getElementById("pasteCapture").addEventListener("paste", handlePaste, false);

( Fiddle )

如果您粘贴到输入框中,它会被捕获并重定向到 DIV。我试图让它在整个页面或容器 div 内工作,但它没有——看起来只有一些元素触发了粘贴事件。

同样当DIV被隐藏时也不起作用,所以需要取消隐藏DIV,然后在粘贴完成后使用超时重新隐藏它。

我不确定您是否可以为您想做的事情工作。

关于javascript - 将内容粘贴到 DIV 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18669695/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com