PS:初步發(fā)現(xiàn)是瀏覽器相容問題,只有IE支援clipboardData,那麼問題來了,有沒有更優(yōu)的方法?
問題闡述:
global.html裡有一個(gè)「id=biao1」的iframe,現(xiàn)欲透過js抽取iframe中p標(biāo)籤的文字內(nèi)容並複製到剪貼簿,本例欲採用clipboardData.setData方法並出現(xiàn)如下報(bào)錯(cuò)訊息,請(qǐng)大牛指點(diǎn)。
報(bào)錯(cuò)訊息:
zhiliangbaobiao.js:8 Uncaught TypeError: Cannot read property 'setData' of undefined
at copysql1 (zhiliangbaobiao.js:8)
at HTMLInputElement.onclick (global.html:172)
global.html 主要原始碼
<iframe id="biao1" src="test00.html" frameborder="0" style="width:100%;height:100%;"></iframe>
<input class="btn btn-success" onclick="copysql1()" value="點(diǎn)擊復(fù)制代碼" type="button">
zhiliangbaobiao 主要源碼
var ifr = document.getElementById('biao1');
var ifrwin = ifr.window || ifr.contentWindow;
var temp = ifrwin.document.getElementsByTagName("p").item(0).innerText;
window.clipboardData.setData("text",temp);