【发布时间】:2020-04-23 00:11:09
【问题描述】:
我创建了一个带有三个按钮的选项卡菜单。传递<div> id 后,我可以复制单个选项卡内容。如何获取活动<div> 的ID,以便通过range.selectNode(document.getElementById(*ID*)) 传递ID 以复制当前活动选项卡的内容?
// function to copy
function CopyToClipboard(){
var range = document.createRange();
range.selectNode(document.getElementById("Cricket"));
window.getSelection().removeAllRanges(); /* clear current selection*/
window.getSelection().addRange(range); /* to select text*/
document.execCommand("copy");
window.getSelection().removeAllRanges();/* to deselect*/
}
function openGame(evt, GameName){
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(GameName).style.display = "block";
evt.currentTarget.className += " active";
}
<button onclick="CopyToClipboard()" >Copy</button>
<p>Click to copy:</p>
<div class="tab">
<button class="tablinks" onclick="openGame(event, 'Cricket')">Cricket</button>
<button class="tablinks" onclick="openGame(event, 'Football')">Football</button>
<button class="tablinks" onclick="openGame(event, 'Chess')">Chess</button>
</div>
<div class="container" id="frame">
<div id="Cricket" class="tabcontent">
<p>Cricket</p>
</div>
<div id="Football" class="tabcontent">
<p>Football</p>
</div>
<div id="Chess" class="tabcontent">
<p>Chess</p>
</div>
</div>
【问题讨论】:
标签: javascript html