javascript - 求教一个js拷贝文本的问题

WBOY
풀어 주다: 2016-06-06 20:22:57
원래의
1028명이 탐색했습니다.

就是简单的点击拷贝,好像很多网站都有复制代码这个按钮。但是好像说很多浏览器不支持,要用flash,现在这个问题一般都是怎么解决的

回复内容:

就是简单的点击拷贝,好像很多网站都有复制代码这个按钮。但是好像说很多浏览器不支持,要用flash,现在这个问题一般都是怎么解决的

long ago都是需要引入flash通过调用flash的api访问clipboard的,巨恶心..既然现在abobe自己都不推荐flash了,自然有办法。

https://zenorocha.github.io/clipboard.js/
depende on Selection and document.execCommand API

<code class="javascript"><script>
function CopyAll(T)
{
T.focus()
T.select()
if (document.all){
therange=T.createTextRange()
therange.execCommand("Copy") //复制
}
}
</script>
<input type="BUTTON" value="复制" onclick="CopyAll(text1)">
<textarea name="text1" cols="40" rows="5">(内容测试)</textarea>
<textarea cols="40" rows="5">(你可以在这里粘帖内容)</textarea></code>
로그인 후 복사

he JavaScript document.execCommand('copy') support has grown, see the links below for browser updates:

IE10+ (although this document indicates some support was there from IE5.5+).
Google Chrome 43+ (~April 2015)
Mozilla Firefox 41+ (shipping ~September 2015)
Opera 29+ (based on Chromium 42, ~April 2015)
Simple Example
var copyTextareaBtn = document.querySelector('.js-textareacopybtn');

copyTextareaBtn.addEventListener('click', function(event) {
var copyTextarea = document.querySelector('.js-copytextarea');
copyTextarea.select();

try {

<code>var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);</code>
로그인 후 복사

} catch (err) {

<code>console.log('Oops, unable to copy');</code>
로그인 후 복사

}
});





详见http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!