3 回答

TA貢獻1836條經驗 獲得超13個贊
textNode在i標記之后獲取并更新其內容。
function pingall(){
// get the i tag within button then get the text content
// after the tag using nextSibling property
var textNode = document.querySelector('#btnShow i').nextSibling;
// check textContetnt property for updating or checking
if ((textNode.textContent).trim() == "Show All") {
textNode.textContent = "Hide All";
} else {
textNode.textContent = "Show All";
}
}
<button id='btnShow' class='btn btn-info pull-right' onclick='pingall()'><i class='fa fa-desktop'></i>Show All</button>

TA貢獻1785條經驗 獲得超4個贊
發生這種情況是因為您在按鈕內有一個圖標元素。
您可以將HTML更新為:
<button id='btnShow' class='btn btn-info pull-right' onclick='pingall()'><i class='fa fa-desktop'></i><span id="btn-text">Show All</span></button>
而JS可以:
var showallButtonText=document.getElementById('btn-text');
if((showallButtonText.innerText).trim()=="Show All"){
showallButtonText.innerHTML="Hide All";
}else{
showallButtonText.innerHTML="Show All";
}

TA貢獻1864條經驗 獲得超2個贊
INNERHTML替換HTML元素內的所有內容。
因此,在您的情況下,它將刪除包括<span>標簽在內的所有內容,并添加“全部隱藏”或“全部顯示”。
只需更換文本,你可以使用ID你的<span>元素。
var textNode = document.querySelector('#idofyourspan');
您也可以將后獲得下一個元素<i>與nextSibling
var textNode = document.querySelector('#btnShow i').nextSibling; // this should be your <span>
完整代碼:
var textNode = document.querySelector('#btnShow i').nextSibling; // get the next sibling after <i> tag inside button
// check textContetnt property for updating or checking
if ((textNode.textContent).trim() == "Show All") {
textNode.textContent = "Hide All";
} else {
textNode.textConten = "Show All";
}
function pingall(){
// your code
}
<button id='btnShow' class='btn btn-info pull-right' onclick='pingall()'><i class='fa fa-desktop'></i>Show All</button>
添加回答
舉報