Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将.innerHTML与字符串进行比较_Javascript_Jquery_Html - Fatal编程技术网

Javascript 将.innerHTML与字符串进行比较

Javascript 将.innerHTML与字符串进行比较,javascript,jquery,html,Javascript,Jquery,Html,我有一个播放/暂停按钮,我想在用户点击它时检查它是播放还是暂停 window.PlayPauseButton = document.getElementById("PlayPauseButton"); PlayPauseButton.onclick = function() //when you click the play or pause button { if( window.PlayPauseButton.innerHTML == "Pause" ) {

我有一个播放/暂停按钮,我想在用户点击它时检查它是播放还是暂停

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";
    }
};

但它不起作用!我无法将innerHTML与字符串进行比较。

您需要检查innerText,而不是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";
    }
};

如果您使用的是按钮标签,请尝试改用
innerText

if (window.PlayPauseButton.innerText == "Pause" ){
    window.PlayPauseButton.innerHTML = "Play";
    clearInterval( window.TheTimer );
}
else  {
    window.PlayPauseButton.innerHTML = "Pause";
}
只有在使用
Play
时,此选项才有效


如果使用的是
,则需要比较值,而不是
innerText

innerHTML
是一个字符串值,与字符串的比较非常好。但是,您既没有包含HTML,也没有包含错误消息,因此人们只能猜测。“它不工作”是对bug最糟糕的描述——你应该总是准确地说出什么没有发生。