gpt4 book ai didi

javascript - 指示另一个选项卡上的内容何时更改

转载 作者:太空宇宙 更新时间:2023-11-04 05:36:50 25 4
gpt4 key购买 nike

我有多个选项卡,但我想在用户未单击的另一个选项卡上发生更改时进行指示。例如,当用户在数据 Pane 中点击运行按钮时,我想将 Errors 选项卡更改为红色,以指示该选项卡也发生了更改 - 我该如何实现这个? (相反,如果它们位于错误选项卡上,则数据选项卡应变为红色)。此外,一旦单击选项卡,我想删除红色。任何帮助表示赞赏!

期望的输出

enter image description here

runCode = () => {
document.getElementById("dataOutput").innerHTML = "Paragraph changed!"
document.getElementById("errorOutput").innerHTML = "Error Occured"
}
.fab {
width: 60px;
height: 60px;
background-color: #A7C0CD;
border-radius: 50%;
box-shadow: 0px 3px 15px rgba(0,0,0,0.2);
transition: all 0.1s ease-in-out;
font-size: 15px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
color: white;
text-align: center;
line-height: 58px;
float: right;
margin: 8px;
z-index: 5000;
position: absolute;
right:0;
top:0;
}

.fab:hover {
box-shadow: 0px 3px 15px rgba(0,0,0,0.2);
transform: scale(1.05);
}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<head>
<!-- Visual appearance -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap.min.js@3.3.5/bootstrap.min.js"></script>
</head>
<body>

<div class="fab" id="runCode" onclick="runCode();">RUN</div>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#data">Data</a></li>
<li><a data-toggle="tab" href="#error">Errors</a></li>
</ul>

<div class="tab-content">
<div id="data" class="tab-pane fade in active">
<div id="dataOutput"></div>
</div>
<div id="error" class="tab-pane fade">
<div id="errorOutput"></div>
</div>
</div>

</body>
</html>

最佳答案

我想我使用 MutationObserver 实现了您想要的。它相当脆弱,可以使用一些重构,但你明白我希望的要点:

// store a reference to our tabs as we're going to re-use them
const data = document.getElementById("dataOutput");
const error = document.getElementById("errorOutput");
const runCode = () => {
data.textContent = "Paragraph changed!";
error.textContent = "Error Occured";
};

// remove the 'changed' class from clicked tabs
const tabs = document.querySelector('.nav-tabs');
tabs.onclick = ({ target }) => target.classList.remove('changed');

// we're going to watch for mutations of our output-elements
new MutationObserver(observe).observe(data, { childList: true });
new MutationObserver(observe).observe(error, { childList: true })

function observe(mutations) {
const target = mutations[0].target;
// get the name of the changed tab by slicing of 'Output' from the target's id
const tabName = target.id.slice(0, target.id.indexOf('O'));
const tab = document.querySelector(`a[href="#${tabName}"]`);

// give the 'changed' class to the tab related to the changed dom-node,
// if it's not currently active
if (!target.parentNode.classList.contains('active')) {
tab.classList.add('changed');
}
}
.fab {
width: 60px;
height: 60px;
background-color: #A7C0CD;
border-radius: 50%;
box-shadow: 0px 3px 15px rgba(0,0,0,0.2);
transition: all 0.1s ease-in-out;
font-size: 15px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
color: white;
text-align: center;
line-height: 58px;
float: right;
margin: 8px;
z-index: 5000;
position: absolute;
right:0;
top:0;
}

.fab:hover {
box-shadow: 0px 3px 15px rgba(0,0,0,0.2);
transform: scale(1.05);
}

a.changed {
color: red
}
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<head>
<!-- Visual appearance -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap.min.js@3.3.5/bootstrap.min.js"></script>
</head>
<body>

<div class="fab" id="runCode" onclick="runCode();">RUN</div>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#data">Data</a></li>
<li><a data-toggle="tab" href="#error">Errors</a></li>
</ul>

<div class="tab-content">
<div id="data" class="tab-pane fade in active">
<div id="dataOutput"></div>
</div>
<div id="error" class="tab-pane fade">
<div id="errorOutput"></div>
</div>
</div>

</body>
</html>

关于javascript - 指示另一个选项卡上的内容何时更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59567686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com