【问题标题】:Change closed div button's text更改关闭的 div 按钮的文本
【发布时间】:2019-02-19 03:40:22
【问题描述】:

我有一个与 toggle() 函数切换的 div 列表。当它切换时,用于切换 div 的按钮会将文本从“+”更改为“-”。但是当我点击下一个按钮 div 或上一个按钮 div 时,旧按钮没有用“-”更改文本(因为已关闭,所以它应该将文本更改为“-”。所以我想更改如果 div 已关闭,则按钮的文本为“-”。

$(".p1").hide();
$(".p1-venue").hide();
$(".p1-img").hide();
$(".btn1").click(function() {
  if ($.trim($(this).text()) === '+') {
    $(this).text('-');
  } else {
    $(this).text('+');
  }
  var $p1 = $(this).next(".p1").toggle();
  var $p1venue = $(this).parents().next(".p1-venue").toggle();
  var $p1img = $(this).parent().parent().parent().parent().find(".p1-img").toggle();
  $(".p1").not($p1).hide();
  $(".p1-venue").not($p1venue).hide();
  $(".p1-img").not($p1img).hide();
});

【问题讨论】:

    标签: jquery html css button


    【解决方案1】:

    使用.is(":visible"),如下所示。

    Click here to find out more

    window.setInterval(() => {
      $(".myDiv").toggle();
    }, 1000);
    
    window.setInterval(() => {
      if ($(".myDiv").is(":visible")) {
        $(".text").html("Visible");
      }
      else {
        $(".text").html("Hidden");
      }
    }, 1000);
    .myDiv {
      width: 100px;
      height: 100px;
      background-color: red;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <p class="text">Visible</p>
    <div class="myDiv"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 2020-08-25
      • 2020-08-25
      • 2015-11-13
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多