【问题标题】:I want a text box to appear on my website if someone uses IE11 [duplicate]如果有人使用 IE11,我希望在我的网站上显示一个文本框 [重复]
【发布时间】:2018-07-27 17:37:09
【问题描述】:

这是我上一个被搁置的帖子的第二个帖子。

我希望在我或其他人在 IE11 中打开我的网站时出现一个文本框,因为我遇到了一些问题,问题得到了解决。但如果再次发生这种情况,我希望再使用这段代码。

在我的last post 中,一个名叫 Zani 的人给出了一个仅对 IE11 有反应的代码,但它没有按应有的方式工作。在我的 IE11 控制台中它说:

SCRIPT5007:无法获取未定义或空引用的属性“样式”。

我把它放在一个 JavaScript 文件还是我把它写成 HTML 都没关系。

代码是:

var isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
if(isIE11)
  document.getElementById("IDOfYourElement").style.display="block";

我通常擅长 HTML 和 CSS,但 Javascript 则完全不同。我不完全确定哪里出了问题。但我认为它与style 有关,并且在我应该编写元素的ID 时,我已将其更改为正确的ID,并且那里没有错误的语法。

编辑:1 这是网站的代码。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
</script>
<link href="/CSS/Incompatible.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/Incompatible.js"></script>
    <script type="text/javascript">
var isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
    if(isIE11)
  document.getElementById("simpleModal").style.display="block";
</script>
</head>
<body>
<button id="modalBtn" class="button" onClick="myModal()">Click
Here</button>
<div id="simpleModal" class="modal">
<div class="modal-content">
  <div class="modal-header">
      <span class="closeBtn" onClick="myModalHide()">&times;</span>
      <h2>Modal Header</h2>
  </div>
  <div class="modal-body">
    <p>Hello...I am a modal</p>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla repellendus nisi, sunt consectetur ipsa velit repudiandae aperiam modi quisquam nihil nam asperiores doloremque mollitia dolor deleniti quibusdam nemo commodi ab.</p>
  </div>
  <div class="modal-footer">
    <h3>Modal Footer</h3>
  </div>
</div>
</div>
</body>
</html>

好的,把它放在 HTML 页面的底部就可以了。但我真的希望 Javascript 位于 javascript 文件中。所以我尝试制作一个准备好的功能,但它不起作用。

【问题讨论】:

  • 也许 ID "IDOfYourElement" 不存在 - 也许 Zani 不知道 ID,并将其留给你,那个 HTML 的作者(你擅长)来替换 @987654326 @ 带有你的元素的ID
  • 我将“IDOfYourElement”更改为 simpleModal。我的代码中有。
  • 您在另一个问题中发布的 HTML 中不存在该 ID????
  • 我现在添加了代码。

标签: javascript html css


【解决方案1】:

错误的意思是没有 ID 为 IDOfYourElement 的元素(或您在此处放置的任何 ID)。所以两件事之一:

  1. 您没有 ID 为 (IDOfYourElement) 的元素或
  2. javascript 文件在元素加载之前加载。

如果情况是 2,那么你应该确保你的 &lt;script&gt; 标签包含在你的 HTML 文件的最底部,或者用一个准备好的函数包装你的 javascript 代码。

编辑

要将您的代码包装在一个现成的函数中,您可以使用 Jquery 或纯 Javascript 来实现。以下是示例

使用纯 Javascript

window.onload = function() {
  // DOM has loaded, you can execute your JS code now.
}

使用 Jquery

$( document ).ready(function() {
  // DOM has loaded, you can execute your JS code now.
});

【讨论】:

  • 这是案例编号 2 我将它添加到底部并且它有效。如果我想要 Javascript 文件中的代码。我如何使准备好的功能?
  • @JessenJonas 我更新了我的答案,向您展示如何在执行 JS 之前等待页面加载。
  • 谢谢!这真的很有帮助!
猜你喜欢
  • 2012-01-18
  • 2015-03-21
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多