【问题标题】:using a hyperlink to change style.display使用超链接更改 style.display
【发布时间】:2020-06-12 22:33:07
【问题描述】:

目前,我有一个带有空 href 的超链接。最初的目标是我有一个输入数字元素,div 的显示发生变化(style.display = 'none';),然后超链接弹出,以便用户可以将显示更改为内联或 flex(style.display = 'inline';) 这是我的 HTML 和 JavaScript,解释的显示更改在 if 语句的 else 部分中

HTML:

<div class="preT" id="preT">
  <form>
    <label for="voipLines">VoIP Lines</label>
    <input type="number" id="voipLines" name="voipLines" required min="1" max="1000" value="1">
    <button class="button" onclick="testfun()">Test Clicker</button>
  </form>
  <div class="voipbutton" id="voipbutton">
  </div>
</div>
<div id="duringT">
  <p Id="clickerTest"></p>
  <div Id="prBar" data-label=""></div>
  <div id="canvascon"></div>
</div>

JavaScript:

function testfun() {
  var voip = document.getElementById("voipLines").value;
  if (voip < 1) { //voiplines verification 
    return document.getElementById("clickerTest").innerHTML = "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>";
  } else if (voip > 1000) {
    return document.getElementById("clickerTest").innerHTML = "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>";
  } else { // Trying to move the button within the form element
    document.getElementById("clickerTest").innerHTML = '<h2 id="testT">Clicker Successful!!!</h2><p>Please click the link below to start <b>' + voip + ' test</b></p><p id="reset">To change the amount of tests to run <a href= "">Click Here</a></p>';
  }

PS。

我打算为此建立一个单独的论坛,但我想如果有人知道这个解决方案,那就太好了。

如果有人对显示的内容有任何想法:

自从我将按钮从&lt;div id = "VoIP button"&gt; 放入&lt;form&gt; 标记后,if 语句就一直在循环。当我输入一个 1000 的数字时,它会提供正确的文本。但是当我输入一个范围内的数字时,画布和以下函数仅显示一秒钟,然后循环返回以显示 &lt;div id="preT"&gt;

【问题讨论】:

    标签: javascript html if-statement input href


    【解决方案1】:

    你离得太近了!在 HTML 5 规范中,表单内的 &lt;button&gt; 具有默认类型 submit 您的 javascript 运行,并且页面立即“提交”以加载下一页。只需添加type="button" 即可防止这种情况并停止提交表单。

    function testfun() {
      var voip = document.getElementById("voipLines").value;
      if (voip < 1) { //voiplines verification 
        return document.getElementById("clickerTest").innerHTML = "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>";
      } else if (voip > 1000) {
        return document.getElementById("clickerTest").innerHTML = "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>";
      } else { // Trying to move the button within the form element
        document.getElementById("clickerTest").innerHTML = '<h2 id="testT">Clicker Successful!!!</h2><p>Please click the link below to start <b>' + voip + ' test</b></p><p id="reset">To change the amount of tests to run <a href= "">Click Here</a></p>';
      }
    }
    <div class="preT" id="preT">
      <form>
        <label for="voipLines">VoIP Lines</label>
        <input type="number" id="voipLines" name="voipLines" required min="1" max="1000" value="1">
        <button class="button" type="button" onclick="testfun()">Test Clicker</button>
      </form>
      <div class="voipbutton" id="voipbutton">
      </div>
    </div>
    <div id="duringT">
      <p Id="clickerTest"></p>
      <div Id="prBar" data-label=""></div>
      <div id="canvascon"></div>
    </div>

    【讨论】:

    • 嗯,好的,现在可以了,感谢您做出非常清晰的解释。但是,为什么使用返回/输入按钮没有做任何事情呢?我想这不是什么大问题,但这就是移动按钮标签的重点。
    • 我认为您正在寻找的不是真正的按钮,而是修改表单的 onsubmit 功能。请查看stackoverflow.com/a/8082928/7314155 了解更多信息。
    • 我根据提供的链接信息做了一些研究,并偶然发现了事件监听器。但同样的事情正在发生。我遇到了一个使用 event.preventDefault(); 的 youtube 视频,并在我的函数开始时添加了它。就像一个魅力,但我不确定这是否会影响功能中的其他任何东西。
    • @mastaSabin 我不想从中获得乐趣。这是正确的解决方案,也是一个很好的学习发现。
    • 我的“导师”形象告诉我,我的 if 语句没有按预期工作,因为变量 voip 是一个字符串,我应该查找 function parseInt() 并将其合并。除此之外,他没有解释太多,您介意解释为什么它需要是整数而不是字符串吗?别介意我不认识除他之外的任何人知道javascript。
    猜你喜欢
    • 2010-12-28
    • 2013-02-04
    • 1970-01-01
    • 2016-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    相关资源
    最近更新 更多