如何在django模板中将变量复制到[剪贴板]
how to copy variable to [clipboard] in django template
如何从 Django 模板的 html 页面中复制变量?
render(request, 'doc.html', {'stack': stack, 'text':text,})
你的问题可能看起来不够清楚,无法描述你想要什么或描述你的问题,但我对解决方案的一些猜测是你需要在你的模板中使用 js 代码,特别是 document.text.select()
和 document.execCommand('copy')
.
也许以下示例就足够了:
在html代码中
<html>
<body>
<button style="background:green; text-align:center;" onclick="CopyText()">Copy Text</button>
<input style="text-align:center;" type="text" value="{{text}}"id="yourtext">
</body>
</html>
javascript:
{% block scripts %}
<script>
function CopyText() {
var text = document.getElementById('yourtext')
text.select();
document.execCommand('copy')
}
</script>
{% endblock scripts %}
在上面的例子中,我们使用你发送给模板的变量的值作为文本标签中的默认值,然后我们通过标签的ID yourtext
指定文本,并且然后执行复制命令。
看似不完美,但足以解决你的问题。
如何从 Django 模板的 html 页面中复制变量?
render(request, 'doc.html', {'stack': stack, 'text':text,})
你的问题可能看起来不够清楚,无法描述你想要什么或描述你的问题,但我对解决方案的一些猜测是你需要在你的模板中使用 js 代码,特别是 document.text.select()
和 document.execCommand('copy')
.
也许以下示例就足够了:
在html代码中
<html>
<body>
<button style="background:green; text-align:center;" onclick="CopyText()">Copy Text</button>
<input style="text-align:center;" type="text" value="{{text}}"id="yourtext">
</body>
</html>
javascript:
{% block scripts %}
<script>
function CopyText() {
var text = document.getElementById('yourtext')
text.select();
document.execCommand('copy')
}
</script>
{% endblock scripts %}
在上面的例子中,我们使用你发送给模板的变量的值作为文本标签中的默认值,然后我们通过标签的ID yourtext
指定文本,并且然后执行复制命令。
看似不完美,但足以解决你的问题。