【问题标题】:How to toggle on/off a particular JavaScript script via onclick of a toggle?如何通过 onclick 切换开关打开/关闭特定的 JavaScript 脚本?
【发布时间】:2021-07-03 13:38:48
【问题描述】:

我正在尝试为我的简单工具使用一项新功能,该功能本质上将添加一种“引导式”使用方式。

它的工作原理是:如果用户点击一个按钮,它会建议(启用)和禁用其他按钮。

现在我意识到有些用户不喜欢这种对工具的受限访问,这就是为什么我想问一下是否可以在设置中添加一个切换来打开/关闭此功能。我希望它默认开启。

简单地说,切换应该意味着:如果它已打开,则允许“引导”功能。如果关闭,请禁用此功能并让用户单击他们想要的任何按钮。

这是我的示例:

function resetall() {
    document.getElementById("2nd").disabled = false;
    document.getElementById("1st").disabled = false;
    document.getElementById("3rd").disabled = false;
    document.getElementById("4th").disabled = false;
}
function disable1st() {
    document.getElementById("1st").disabled = true;
    document.getElementById("2nd").disabled = false;
    document.getElementById("3rd").disabled = true;
    document.getElementById("4th").disabled = true;
}

function disable2nd() {
    document.getElementById("2nd").disabled = true;
    document.getElementById("1st").disabled = false;
    document.getElementById("3rd").disabled = false;
    document.getElementById("4th").disabled = true;
}
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<button id="reset" onclick="resetall()">Reset</button>

<br><br>

<button id="1st" onclick="disable1st()">1</button>
<button id="2nd" onclick="disable2nd()">2</button>
<button id="3rd" onclick="disable3rd()">3</button>
<button id="4th" onclick="disable4th()">4</button>


<br><br>

<!---This toggle button--->

<label class="switch">
  <input type="checkbox" checked>
  <span class="slider round"></span>
</label>

如果我没有太多代码可以显示,我想提前道歉。我是编码新手,还在学习……但如果可能的话,我真的很想拥有这个功能。在此先感谢您,如果有任何反馈/建议,我将不胜感激。

编辑:

希望能进一步解释我的问题,这是该工具的屏幕截图。

如您所见,有几个按钮在单击时会做出反应,将特定文本插入到文本区域中,并且更重要的是禁用任何按钮' t 适合作为单击按钮的延续。

现在我正在寻找一个可以打开/关闭此“引导”模式的功能,可能通过使用复选框(如果勾选或未勾选)或切换,重要的是打开上面附加的脚本。

如果用户不想拥有此功能,则应打开/关闭“引导模式”。因此,他们应该能够随意点击任何按钮,而不受禁用按钮的限制。

上面提供的脚本是一个更简单的版本,但它正是我需要并一直在使用的。我只是希望能够通过切换打开/关闭它而无需编码。

【问题讨论】:

  • 那么您的问题到底是什么?看起来您有一些相关的代码或多或少地在做您想做的事情。虽然从它的声音来看,您需要验证 disable1st() 只是禁用 1st,而不是像当前编码的那样禁用 3rd4th。您还需要为您的切换创建一个事件侦听器,以调用resetall()disable1st()
  • 对此我深表歉意。我的问题是:如何让这个蓝色切换开关打开/关闭它上面的功能?这样整个脚本如果关闭就无法工作,反之亦然?
  • stackoverflow.com/questions/14544104/… 是您正在寻找的。您可以检查事件侦听器中的值,然后做任何您想做的事情 - 例如if (this.checked) { disable1st(); } else { resetall(); }.
  • 我还建议从您的 HTML 中删除 onclick="disable1st()",而是将其绑定到 JS 中,例如 stackoverflow.com/a/6348597/1499877。这使您能够在用户单击按钮时添加更多功能。
  • 为每个在使用指南时应禁用的按钮添加一个类,然后当您单击指南切换按钮时,您可以整体启用/禁用所有这些按钮

标签: javascript html jquery


【解决方案1】:

有点不清楚您要完成什么。我把这个使用 jQuery 的例子放在一起。

$(function() {
  function resetAll(event) {
    $("button:not('#reset')").prop("disabled", false)
      .removeClass("disabled")
      .addClass("enabled");
  }

  function disableButton(obj) {
    $(obj).prop("disabled", true).toggleClass("enabled disabled");
  }

  resetAll();

  $("button:not('#reset')").click(function(event) {
    if ($(".switch > input[type='checkbox']").is(":checked")) {
      if ($(this).is("#1st")) {
        resetAll();
        disableButton("#1st, #3rd, #4th");
      }
      if ($(this).is("#2nd")) {
        resetAll();
        disableButton("#2nd, #4th");
      }
    } else {
      disableButton(this);
    }
  });

  $("#reset").click(resetAll);
});
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked+.slider {
  background-color: #2196F3;
}

input:focus+.slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked+.slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

button {
  border: 1px outset #6c6c6c;
  background-color: #eee;
  padding: .4em 1.6em;
  border-radius: 6px;
  font-weight: 600;
}

button:hover {
  background-color: #ccf;
}

button.disabled {
  background-color: #ccc;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="reset">Reset</button>
<div style="margin: 1em 0;">
  <button id="1st">1</button>
  <button id="2nd">2</button>
  <button id="3rd">3</button>
  <button id="4th">4</button>
</div>
<!---This toggle button--->
<label class="switch">
  <input type="checkbox" checked>
  <span class="slider round"></span>
</label>

更新

我添加了一个条件,如果 Toggle 为 On,单击将触发禁用。如果 Toggle 为 Off,则不会触发禁用。

【讨论】:

  • 您好!感谢您抽出宝贵时间将这一切放在一起。我想要完成的是:blue toggle 应该是一个开关。一个开关,将激活/停用disable 按钮 onclick 的功能。因此,如果blue toggle 关闭,按钮的禁用功能 onclick 应该不起作用,用户应该能够单击没有禁用功能的任何按钮。
  • 对不起,如果我不能以更好的方式解释它。现在,如果用户单击1,它将被禁用或其他指定的按钮被禁用。 blue toggle 的目的应该是关闭该功能。它们中的任何一个都没有禁用的按钮。如果开启,该功能将恢复。
  • @ixcode 好的,如果 Toggle 关闭了功能并且用户单击了按钮会发生什么?
  • @ixcode 这只是一个更复杂的条件。从你的描述还是很难判断的。您可能需要制作状态图或流程图来帮助阐明您的活动。
  • @ixcode 我最近的更新对你有用。基本上,在您的点击事件中,您将检查您的开关是否在引导模式下打开/关闭,这将定义点击、引导或非引导活动的行为。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2023-01-31
  • 2016-09-22
  • 2021-03-11
相关资源
最近更新 更多