我创建了一个带有三个按钮的选项卡菜单。我可以在传递 id 后复制单个选项卡内容<div>。如何获取活动选项卡的 ID <div>,以便我可以通过该 ID 来range.selectNode(document.getElementById(*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>
呼啦一阵风
拉莫斯之舞
qq_花开花谢_0
HUX布斯
相关分类