【问题标题】:Why isn't my hidden DIV being displayed at all?为什么我隐藏的 DIV 根本不显示?
【发布时间】:2017-12-22 19:43:29
【问题描述】:

我有一个必须重定向用户的表单(Salesforce 重定向要求的一部分)。我想将用户保留在页面上,因此重定向链接与添加了“?submitted = 1”的页面相同。我已经尝试了很多在网上找到的不同脚本来检查提交的 url,然后将 div 从无更改为阻止。它不起作用,我不确定为什么。这是我拥有的最新代码,但请记住,我一直在从这里和网络上搜索并尝试各种答案,所以我可能已经尝试过其他方法。

这是我的 HTML:

<div id="savingsSuccess">
    <div id="alertSuccess" class="alert alert-success">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Your inputs have been sent to our team, we will send your savings estimate to email</strong>
    </div>
</div>

我的 CSS 在这里:

#alertSuccess {
    display: none;
}

我的脚本如下:

<script type="text/javascript">
   if (/submitted/.test(window.location.href)) {
      document.getElementByID('alertSuccess').style.display = 'block';
   }
</script>

我做错了什么?

【问题讨论】:

  • 你的 script 标签是在你的 div 标签之前还是之后?如果在 div 存在之前运行您的 JavaScript 之前...
  • 您使用 href 的正则表达式测试看起来不错。当脚本进入if语句时,你知道#alertSuccess是否存在吗?我会从那里开始。
  • 是的,脚本在 div 之后。但是,我使用的是getElementByID 而不是getElementById

标签: javascript html css forms


【解决方案1】:

您拼错了方法getElementById,必须是Id 而不是ID

Here 是文档。

【讨论】:

    【解决方案2】:

    我对你的脚本做了一些修改。

    首先:ID 不是 ID,只是为了让事情不那么骇人听闻,我继续对 URL 进行拆分/子串化

    HTML:

    <div id="savingsSuccess">
        <div id="alertSuccess" class="alert alert-success">
          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Your inputs have been sent to our team, we will send your savings estimate to email</strong>
        </div>
    </div>
    

    CSS 在这里:

    #alertSuccess {
        display: none;
    }
    

    脚本

    <script type="text/javascript">
       if (window.location.href.substring(window.location.href.lastIndexOf("?"),window.location.href.length-1).split("&").indexOf("submitted=1")==-1) {
          document.getElementById('alertSuccess').style.display = 'block';
       }
    </script>
    

    【讨论】:

    • 你所说的“hackish”是什么意思?你看到的代码,我在我在这里找到的一个搜索结果中找到的。
    • 文件路径中包含“提交”字样怎么办?
    • 这是一个小改动,但它是一个选项
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    相关资源
    最近更新 更多