Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.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 在按钮jquery中更改和更改回文本_Javascript_Jquery - Fatal编程技术网

Javascript 在按钮jquery中更改和更改回文本

Javascript 在按钮jquery中更改和更改回文本,javascript,jquery,Javascript,Jquery,我希望有一个jquery按钮事件,第一次单击按钮时,会出现一个div(使用class.explainion)并更改文本,第二次单击时,会隐藏该div,并将文本更改回原始文本。我可以做第一部分,但第二部分不行。在下面的示例中,将-(切换类(“隐藏”)-更改为-.fadeOut…-也不起作用,因此我认为我的if和else有问题。提前感谢 $("button:nth-of-type(1)").click(function() { if (this.text = "Read Explanation"

我希望有一个jquery按钮事件,第一次单击按钮时,会出现一个div(使用class
.explainion
)并更改文本,第二次单击时,会隐藏该div,并将文本更改回原始文本。我可以做第一部分,但第二部分不行。在下面的示例中,将-(
切换类(“隐藏”)
-更改为-
.fadeOut…
-也不起作用,因此我认为我的
if
else
有问题。提前感谢

$("button:nth-of-type(1)").click(function() {
  if (this.text = "Read Explanation") {
    $(".explanation").fadeIn("slow", function() {});
    $(this).text("Hide Explanation");
  } else {
    $(this).text("Read Explanation");
    $(".explanation").toggleClass("hidden");
  }
});
改变

if(this.text=“阅读解释”){

if($(this).text()=“阅读解释”){

演示:

$(“按钮:第n个类型(1)”)。单击(函数(){
if($(this.text()=“阅读说明”){
//$(“.explainion”).fadeIn(“慢”,function(){});
$(此).text(“隐藏解释”);
}否则{
$(此).text(“阅读解释”);
}
$(“.explain”).toggleClass(“隐藏”);
});
。隐藏{
显示:无;
}

阅读解释

说明
您的代码中有几个问题:

  • 应为
    $(此)
  • text
    应该是
    text()
  • if
    条件下使用赋值运算符。它应该是比较运算符
    =
    或严格比较运算符
    =
  • 您可以在
    if-else
    块之外切换
    hidden
  • $(“按钮:第n个类型(1)”)。单击(函数(){
    if($(this.text()=“阅读说明”){
    $(此).text(“隐藏解释”);
    }否则{
    $(此).text(“阅读解释”);
    }
    $(“.explain”).toggleClass(“隐藏”);
    });
    。隐藏{
    显示:无;
    }
    
    一些解释
    
    阅读解释
    您不能根据可能发生变化的数据做出决策。
    如果按钮的文本更改,则需要更改代码。 这样,如果按钮的文本发生更改,则需要更改数据。 这不是最好的选择,但可能会奏效


    谢谢大家。这似乎是我所拥有的最简单/最接近的。我按照Ankit的建议进行了更改: 此为$(此) 文本到文本() =到== 使用-fade out-而不是-hide-有帮助

        $( "button:nth-of-type(1)" ).click(function() {
    if($(this).text() == "Read Explanation") {
        $(".explanation").fadeIn( "slow", function() {
        });
        $(this).text("Hide Explanation");
        } else {
        $(this).text("Read Explanation");
        $(".explanation").fadeOut( "slow", function() {
        });
        }
    });
    

    你能写你的HTML吗?为什么你要编辑很多次,而不是花几分钟去思考和提供一个正确的答案。@AnkitAgarwal,添加解释
        $( "button:nth-of-type(1)" ).click(function() {
    if($(this).text() == "Read Explanation") {
        $(".explanation").fadeIn( "slow", function() {
        });
        $(this).text("Hide Explanation");
        } else {
        $(this).text("Read Explanation");
        $(".explanation").fadeOut( "slow", function() {
        });
        }
    });