Javascript JS更改文本onClick然后更改回onClick If…else

Javascript JS更改文本onClick然后更改回onClick If…else,javascript,text,onclick,Javascript,Text,Onclick,这里有一个简单的 我正在创建一个链接,其中的内容在单击时会发生变化。Onload应该是“”单击此处了解更多信息!当点击时,它应该说“点击这里获取更少的信息!“然后重新单击“…更多信息” 我肯定我只是在某个地方犯了个小错误,帮帮忙 JavaScript <script> function change_text() { if(document.getElementById("toggle_button").innerHTML="Click here for more inform

这里有一个简单的

我正在创建一个链接,其中的内容在单击时会发生变化。Onload应该是“”单击此处了解更多信息!当点击时,它应该说“点击这里获取更少的信息!“然后重新单击“…更多信息”

我肯定我只是在某个地方犯了个小错误,帮帮忙

JavaScript

<script>
function change_text()
{
if(document.getElementById("toggle_button").innerHTML="Click here for more   
information!"){
document.getElementById("toggle_button").innerHTML="Click here for less
information!";
}else{
    document.getElementById("toggle_button").innerHTML="Click here for more     
information!";
}}
</script> 

函数更改_text()
{
if(document.getElementById(“toggle_button”).innerHTML=“单击此处了解更多信息
信息!){
document.getElementById(“切换按钮”).innerHTML=“单击此处了解更多信息
信息;
}否则{
document.getElementById(“切换按钮”).innerHTML=“单击此处了解更多信息
信息;
}}
这是HTML

<a href='javascript: toggle()'><p id="toggle_button" onclick="change_text()">Click here  
for more information!</p></a>    

应该是

if(document.getElementById("toggle_button").innerHTML == "Click here for more   
information!"){
您正在分配而不是比较,因此请使用
=
而不是
=

应该是

if(document.getElementById("toggle_button").innerHTML == "Click here for more   
information!"){
您是在分配而不是比较,因此请使用
==
而不是
=

中缺少“=”

请尝试以下代码:

<script>
function change_text()
{
if(document.getElementById("toggle_button").innerHTML=="Click here for more   
information!"){
document.getElementById("toggle_button").innerHTML="Click here for less
information!";
}else{
    document.getElementById("toggle_button").innerHTML="Click here for more     
information!";
}}
</script>

函数更改_text()
{
如果(document.getElementById(“toggle_button”).innerHTML==”单击此处了解更多信息
信息!){
document.getElementById(“切换按钮”).innerHTML=“单击此处了解更多信息
信息;
}否则{
document.getElementById(“切换按钮”).innerHTML=“单击此处了解更多信息
信息;
}}
您在中丢失了“=”

请尝试以下代码:

<script>
function change_text()
{
if(document.getElementById("toggle_button").innerHTML=="Click here for more   
information!"){
document.getElementById("toggle_button").innerHTML="Click here for less
information!";
}else{
    document.getElementById("toggle_button").innerHTML="Click here for more     
information!";
}}
</script>

函数更改_text()
{
如果(document.getElementById(“toggle_button”).innerHTML==”单击此处了解更多信息
信息!){
document.getElementById(“切换按钮”).innerHTML=“单击此处了解更多信息
信息;
}否则{
document.getElementById(“切换按钮”).innerHTML=“单击此处了解更多信息
信息;
}}

用这个解决你的问题,它会帮助你更好-

  • 检查相等使用“==”
  • javascript:void(0)在href上使用

            <html>
              <head>
    
                <title>index</title>
    
              </head>
              <body>
               <script type="text/javascript">
                    function change_text()
                        {
                            if(document.getElementById("toggle_button").innerHTML=="Click here for more information!")
                            {
                                document.getElementById("toggle_button").innerHTML="Click here for less information!";
                            }
                            else
                            {
                                document.getElementById("toggle_button").innerHTML="Click here for more information!";
                            }
                        }
            </script>
             <a href='javascript:void(0)' onclick="change_text()"><p id="toggle_button" >Click here for more information!</p></a>  
    
              </body>
            </html>
    
    
    指数
    函数更改_text()
    {
    if(document.getElementById(“toggle_button”).innerHTML==“单击此处获取更多信息!”)
    {
    document.getElementById(“toggle_button”).innerHTML=“单击此处查看更多信息!”;
    }
    其他的
    {
    document.getElementById(“toggle_button”).innerHTML=“单击此处获取更多信息!”;
    }
    }
    

  • 用这个解决你的问题,它会帮助你更好-

  • 检查相等使用“==”
  • javascript:void(0)在href上使用

            <html>
              <head>
    
                <title>index</title>
    
              </head>
              <body>
               <script type="text/javascript">
                    function change_text()
                        {
                            if(document.getElementById("toggle_button").innerHTML=="Click here for more information!")
                            {
                                document.getElementById("toggle_button").innerHTML="Click here for less information!";
                            }
                            else
                            {
                                document.getElementById("toggle_button").innerHTML="Click here for more information!";
                            }
                        }
            </script>
             <a href='javascript:void(0)' onclick="change_text()"><p id="toggle_button" >Click here for more information!</p></a>  
    
              </body>
            </html>
    
    
    指数
    函数更改_text()
    {
    if(document.getElementById(“toggle_button”).innerHTML==“单击此处获取更多信息!”)
    {
    document.getElementById(“toggle_button”).innerHTML=“单击此处查看更多信息!”;
    }
    其他的
    {
    document.getElementById(“toggle_button”).innerHTML=“单击此处获取更多信息!”;
    }
    }
    

  • 您不仅将
    =
    误用为
    ==
    ,而且还可以使用一种简单的技术:缓存来极大地改进代码

    function change_text() {
        var button = document.getElementById('toggle_button');
    
        if (button.innerHTML === "Click here for more information!") {
            button.innerHTML = "Click here for less information!";
        }
        else {
            button.innerHTML = "Click here for more information!";
        }
    }
    

    您可以看到代码变得多么清晰。

    您不仅将
    =
    误用为
    ==
    ,而且还可以通过一种简单的技术大大改进代码:缓存

    function change_text() {
        var button = document.getElementById('toggle_button');
    
        if (button.innerHTML === "Click here for more information!") {
            button.innerHTML = "Click here for less information!";
        }
        else {
            button.innerHTML = "Click here for more information!";
        }
    }
    

    您可以看到代码变得多么清晰。

    感谢您的回复。非常感谢您的回复,它的效果非常好!感谢您的回复。非常感谢您的回复,它的效果非常好!感谢您的回复。非常感谢!感谢您的回复。非常感谢!非常感谢您的回复。非常感谢!感谢您的回复回复。非常感谢!感谢您的回复。非常感谢!感谢您的回复。非常感谢!感谢您的回复。非常感谢!感谢您的回复。非常感谢!