使用if else语句判断动作

问题描述 投票:0回答:1

我用的是jquery draggable,想用if else语句判断点击按钮时弹出不同的弹窗,但是不行,怎么办?

$(".btn-popup").on("click", () => {
  if ($(".year2").mouseout()) {
    $(".popup-1").show();
  } else if ($(".year58").mouseout()) {
    $(".popup-2").show();
  }
});
javascript html jquery css
1个回答
0
投票

mouseout()
函数不返回布尔值,因此您的 if 语句不会按预期工作。其次,您需要使用
mouseenter
事件而不是
mouseout
来检测鼠标何时进入年元素。最后,您需要使用可拖动事件来检测用户何时将可拖动元素放到年份元素上。

试试这个。

$(".btn-popup").on("click", () => {
  if ($(".year2").hasClass("dropped")) {
    $(".popup-1").show();
  } else if ($(".year58").hasClass("dropped")) {
    $(".popup-2").show();
  }
});

$(".draggable").draggable({
  revert: "invalid",
  stop: function(event, ui) {
    const year = $(this).data("year");
    const dropped = $(`#year${year}`).hasClass("dropped");
    if (!dropped) {
      $(this).css({ top: 0, left: 0 });
    }
  }
});

$(".year").droppable({
  accept: ".draggable",
  drop: function(event, ui) {
    const year = $(this).data("year");
    $(`#year${year}`).addClass("dropped");
  },
  out: function(event, ui) {
    const year = $(this).data("year");
    $(`#year${year}`).removeClass("dropped");
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.