1 回答

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊
編輯:
一種方法是在 CSS 中將內(nèi)容聲明為 var,如下所示:
content:var(--content,"+");
然后定位并更改內(nèi)容,如下所示:
div.style.setProperty("--content", "'-'")
此外,回答評(píng)論中有關(guān)將內(nèi)容切換到之前的“+”狀態(tài)的其他問(wèn)題。只需應(yīng)用一個(gè)boolean您可以為每次點(diǎn)擊更改的值,如下所示:
var bool = false;
if (bool === false) {
div.style.setProperty("--content", "'-'")
bool = true;
} else if (bool === true) {
div.style.setProperty("--content", "'+'")
bool = false;
}
通過(guò)上面的代碼,我們可以根據(jù)我們聲明為 的布爾值有效地更改內(nèi)容字符串bool。當(dāng)true我們看到+,當(dāng)false我們看到-。
請(qǐng)參閱下面的片段:
var div = document.querySelector('.toggle-box + label');
var bool = false;
function changer() {
if (bool === false) {
div.style.setProperty("--content", "'-'")
bool = true;
} else if (bool === true) {
div.style.setProperty("--content", "'+'")
bool = false;
}
}
.toggle-box+label:after {
background-color: #3b434a;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
color: #FFFFFF;
content: var(--content, "+");
font-weight: bold;
height: 12px;
margin-left: 5px;
text-align: center;
padding: 0px 2px;
}
<div class="VINDesc">
<input class="toggle-box" id="toggle" onclick="changer()">
<label class="VINDesc-label" for="toggle">foo?</label>
<p>NEW TEXT.</p>
</div>
添加回答
舉報(bào)