html代碼
<div class="sjsj"><span>詳細說明:</span>
<div onclick="copyContent(this)">{$show.article_content|raw}</div>
</div>
<input id="copy_content" type="text" value="" style="position: absolute;top: 0;left: 0;opacity: 0;z-index: -10;"/>
js代碼
<script type="text/javascript">
function copyContent(ElementObj){
//獲取點擊的值
var clickContent = ElementObj.innerText;
//獲取要賦值的input的元素
var inputElement = document.getElementById("copy_content");
//給input框賦值
inputElement.value = clickContent;
//選中input框的內容
inputElement.select();
// 執行瀏覽器復制命令
document.execCommand("Copy");
//提示已復制
alert('已復制');
}
</script>
select() 方法只對 input
和 textarea
有效,所以,要獲取到點擊的值,放到input標簽中,再選中復制。