javascript - JS IE 错误(对象不支持属性或方法“选择”)
问题描述
试图将一些信息复制到剪贴板。
在 Chrome 和其他工作正常,在 IE 中我得到错误:“对象不支持属性或方法'select'”
html:
<textarea name="wtBodyInpt" id="wtBodyInpt">Copy This</textarea>
JS:
function copyBodyToClipboard (BodyInpt) {
// Select text inside element
BodyInpt.focus();
BodyInpt.select();
// Copy text to clipboard
document.execCommand('copy');
}
copyBodyToClipboard(wtBodyInpt)
解决方案
您需要通过使用其 ID 或名称访问元素来创建元素的对象。
例子:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>Try to paste the text by pressing ctrl + v in a different window, to see the effect.</p>
<textarea name="wtBodyInpt" id="wtBodyInpt">Copy This Text</textarea>
<p>The document.execCommand() method is not supported in IE8 and earlier.</p>
<script>
function copyBodyToClipboard (element) {
var copyText = document.getElementById(element);
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
}
var str="wtBodyInpt";
copyBodyToClipboard (str)
</script>
</body>
</html>
在 IE 11 浏览器中输出:
推荐阅读
- graphql - NestJS + GraphQL 联合和模式优先 GraphQLDefinitionsFactory
- python - 在 pandas DataFrame 列中使用字符串格式
- flutter - 按下后图标不改变 - Flutter
- python - python redis更新复杂对象(嵌套)(HMSET)
- reactjs - GA 没有看到动态标题标签
- python-3.x - Python 进口和芹菜工人
- javascript - 我应该在javascript中的全局对象中放置一个函数声明吗
- angular - 无需输入登录名和密码即可访问 SSRS 报告
- python - 如何在 Google App Engine for Python3 上安装库
- python - 如果该行中的字符串与python匹配,如何打印变量内的整行文本?