I have a play/pause button and I want to check whether its text is "Play" or "Pause" when the user clicks on it.
window.PlayPauseButton = document.getElementById("PlayPauseButton");
PlayPauseButton.onclick = function() //when you click the play or pause button
{
if( window.PlayPauseButton.innerHTML == "Pause" )
{
window.PlayPauseButton.innerHTML = "Play";
clearInterval( window.TheTimer );
}
else
{
window.PlayPauseButton.innerHTML = "Pause";
}
};
But it doesn't work! I can't pare innerHTML with a string.
I have a play/pause button and I want to check whether its text is "Play" or "Pause" when the user clicks on it.
window.PlayPauseButton = document.getElementById("PlayPauseButton");
PlayPauseButton.onclick = function() //when you click the play or pause button
{
if( window.PlayPauseButton.innerHTML == "Pause" )
{
window.PlayPauseButton.innerHTML = "Play";
clearInterval( window.TheTimer );
}
else
{
window.PlayPauseButton.innerHTML = "Pause";
}
};
But it doesn't work! I can't pare innerHTML with a string.
Share edited Feb 7, 2022 at 14:13 Adil Hussain 32.3k23 gold badges125 silver badges166 bronze badges asked Jun 2, 2016 at 23:45 BenBen 1031 gold badge3 silver badges8 bronze badges 1-
innerHTML
is a string value and pares to strings quite nicely. However, you have not included the HTML nor the error message, so all someone can do is guess. "It doesn't work" is the worst description of a bug -- you should always say exactly WHAT isn't happening. – Jeremy J Starcher Commented Jun 2, 2016 at 23:56
2 Answers
Reset to default 4You need to check with innerText, not innerHTML
PlayPauseButton.onclick = function() //when you click the play or pause button
{
if( window.PlayPauseButton.innerText== "Pause" )
{
window.PlayPauseButton.innerText= "Play";
clearInterval( window.TheTimer );
}
else
{
window.PlayPauseButton.innerText= "Pause";
}
};
If you're using a button tag, try using innerText
instead:
if (window.PlayPauseButton.innerText == "Pause" ){
window.PlayPauseButton.innerHTML = "Play";
clearInterval( window.TheTimer );
}
else {
window.PlayPauseButton.innerHTML = "Pause";
}
This will only work if you're using <button>Play</button>
.
If you're using an <input type="button">
you're going to need to pare the value instead of innerText
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745059268a4608864.html
评论列表(0条)