简体   繁体   English

单击SlideToggle jQuery后更改div名称

[英]change the div name after click on SlideToggle jquery

I have this slideToggle code. 我有这个slideToggle代码。 When I click the button that says "open", it changes the text to "close" but the problem It stays as "close" even if you click it again so I want to change the text back to "open" when I click on "close" 当我单击显示为“打开”的按钮时,它将文本更改为“关闭”,但问题仍然存在,即使您再次单击该按钮也是如此,因此我想在单击时将文本更改回“打开” “关”

this code was working perfectly but now its not working with no reason. 这段代码运行正常,但现在却没有任何理由。

<script>
    $(document).ready(function () {
        $("#open").click(function () {
             $("#close").slideToggle("slow");
             var t=$("#open").html()==="close"?"open:close";
             $("#open").html(t);
             reset ();
        });
    });
</script>


<div id="open" align="center"><p>open</p></div>
<div id="close"> <p>dddddddddddd</p></div>

<style type="text/css">

    #open {
        background:blue;
        height:25px;
        color:white;
    }

    #close {
        background:green;
        height:300px;
        color:white;
        display:none;
    }
</style>

try using .text() instead of .html() 尝试使用.text()而不是.html()

also a better approach may be to toggle a class name on the container, and base your function on that, rather than on the inner text. 还有一种更好的方法可能是在容器上切换一个类名,并使函数基于该名称,而不是基于内部文本。

your updated jquery click handler would look something like this: 您更新的jquery点击处理程序将如下所示:

$('#toggler').click(function(){
  $(this).next('.textBit').slideToggle().toggleClass('open');

  if($(this).next('.open').length){
    $(this).text('closed');
  }
  else{
    $(this).text('open');
  }

});

heres a quick slideToggle demo that you can feel free to edit 这是一个快速的幻灯片切换演示 ,您可以随时对其进行编辑

and this is a version using a ternary operator 这是使用三元运算符版本

Use this instead: 使用此代替:

$("#open").click(function(){
            $("#close").slideToggle("slow"); 
            $("#open").text($(this).text()=="open" ? "close" : "open");
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM