JS IE 错误(对象不支持 属性 或方法 'select')
JS IE Error (Object doesn't support property or method 'select')
正在尝试将一些信息复制到剪贴板。
在 Chrome 和其他人中工作正常,在 IE 中我得到错误:"Object doesn't support property or method '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 浏览器中的输出:
正在尝试将一些信息复制到剪贴板。
在 Chrome 和其他人中工作正常,在 IE 中我得到错误:"Object doesn't support property or method '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 浏览器中的输出: