簡體   English   中英

將Javascript復制到剪貼板,包括html格式的文本

[英]Javascript copy to clipboard including html formatted text

我對js比較陌生,因此需要幫助,

我有一個在php中生成的表格。 我希望用戶能夠單擊一個按鈕並將飛行結果復制到剪貼板,

我具有以下javascript函數:

<script>

function copyToClipboard(element) {
var $temp = $("<textarea>");
var brRegex = /<br\s*[\/]?>/gi;
$("body").append($temp);
$temp.val($(element).html().replace(brRegex, "\r\n")).select();
document.execCommand("copy");
$temp.remove();
}

</script>

但是,當您粘貼結果時,我得到以下帶有可見的格式標記的內容:

<b>Mon 09 Oct - DY 7015 </b> 
Depart: London Gatwick Airport,  (LGW) at 17:05
Arrive: John F Kennedy Airport, New York (JFK) at 20:05 

我希望結果輸入為

10月9日星期一-DY 7015
出發:倫敦蓋特威克機場(LGW),17:05
到達:紐約肯尼迪國際機場(JFK),時間20:05

或如果這不容易實現,那么至少顯示時不格式化但也不顯示標簽

10月9日星期一-DY 7015
出發:倫敦蓋特威克機場(LGW),17:05
到達:紐約肯尼迪國際機場(JFK),時間20:05

有任何想法嗎?

您可以嘗試使用此正則表達式刪除HTML標記: /<\\/?[a-zA-Z]+\\/?>/g

所以,這應該工作:

$(element).html().replace(brRegex, "\r\n").replace(/<\/?[a-zA-Z]+\/?>/g, '')

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM