javascript document.getElementByID 在 IE 8 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11877034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
document.getElementByID Not Working in IE 8
提问by Kenton de Jong
I have never done this before so I apologize if I don't describe my issue good enough or don't use all the right syntax.
我以前从未这样做过,所以如果我没有很好地描述我的问题或没有使用所有正确的语法,我深表歉意。
What I have on my website is an email form. The user can type in a message, along with their name and some other data. Once they finish filling out the form they can submit it and it goes to my email and all is well. Because the form has JavaScript in it, I automatically had it hidden with CSS and then made it visible through JavaScript. That way if the user has their JavaScript turned off the form won't appear.
我的网站上有一个电子邮件表格。用户可以输入消息以及他们的姓名和一些其他数据。一旦他们填写完表格,他们就可以提交它,它会发送到我的电子邮件中,一切都很好。因为表单中有 JavaScript,所以我自动用 CSS 隐藏它,然后通过 JavaScript 使其可见。这样,如果用户关闭了 JavaScript,表单就不会出现。
And it works great. And I thought it displayed great too. I checked it across all my local testing browsers (Chrome 21, FF 14, IE 9, Opera 12 and Safari 5) as well as through Adobe Browser lab for IE 8, 7 and 6. In all of them it looked fantastic. I even checked it on my Android phone and my girlfriend's Blackberry. It looked fine.
而且效果很好。我认为它也显示得很好。我在所有本地测试浏览器(Chrome 21、FF 14、IE 9、Opera 12 和 Safari 5)以及 Adobe 浏览器实验室对 IE 8、7 和 6 进行了检查。在所有这些浏览器中,它看起来都很棒。我什至在我的安卓手机和我女朋友的黑莓手机上检查过。看起来不错。
Until I tested it on my mom's computer with IE 8. Although in Adobe Browserlab it says the form displays, in a real test the form doesn't show up.
直到我在我妈妈的电脑上用 IE 8 测试它。虽然在 Adobe Browserlab 中它说表单显示,但在真正的测试中表单没有显示。
You can test it for yourself here.
您可以在这里亲自测试。
The issue is with the form id, "emailForm". My Form has the markup like so:
问题在于表单 ID,“emailForm”。我的表单有这样的标记:
<form id="emailForm" method="POST" action="../javascript/email_form.php" onSubmit="return validateForm();">
<label id="emailSubjectLabel">Email Subject*:</label><input class="form" title="Email Subject" type="text" name="subject" id="emailSubject"><br>
<label id="nameLabel">Your Name*:</label><input class="form" title="Your Name" type="text" name="name" id="yourName"><br>
<label id="emailLabel">Your Email:</label><input class="form" title="Your Email Address" type="text" name="email" id="yourEmail"><br>
<label id="messageLabel">Your Message*:</label>
<textarea class="form" title="Write Something..." name="message" id="message" cols="40" rows="5"></textarea>
<input class="button" type="submit" value="Submit">
<p class="error">* required.</p>
</form>
My CSS has a whole bunch of code, but it has this in regards to making the form invisible:
我的 CSS 有一大堆代码,但它有关于使表单不可见的代码:
#emailForm, #javaText {
display: none;
}
(note: #javaText is some text on the screen that also is hidden unless the user has JavaScript installed. It also doesn't work in IE 8. Just for simplicity I'm not mentioning it.)
(注意:#javaText 是屏幕上的一些文本,除非用户安装了 JavaScript,否则它也会隐藏。它在 IE 8 中也不起作用。为了简单起见,我没有提到它。)
And the JavaScript looks like this:
JavaScript 看起来像这样:
document.getElementById("emailForm").style.display = "block";
document.getElementById("javaText").style.display = "inline";
function validateForm() {
//function that validates the form
}
I've placed the JavaScript at the bottom of the page, hoping it would help. I've tried changing the style of the content right on the main HTML page, I've checked my mother's JavaScript and it is enabled. I just don't know why it isn't working.
我已将 JavaScript 放在页面底部,希望对您有所帮助。我已经尝试在主 HTML 页面上更改内容的样式,我已经检查了我母亲的 JavaScript 并且它已启用。我只是不知道为什么它不起作用。
If somebody could tell me what I'm doing wrong, I'd be very thankful. This has been giving me a headache for a week now.
如果有人能告诉我我做错了什么,我将不胜感激。这让我头痛了一个星期。
(And yes, I've told her IE sucks and she should go with Chrome, but the reason she doesn't is a whole different story...)
(是的,我告诉她 IE 很糟糕,她应该使用 Chrome,但她不这样做的原因是一个完全不同的故事......)
Thanks for your help. I can't wait to hear what the solution is!
谢谢你的帮助。我迫不及待想知道解决方案是什么!
回答by jfriend00
IE8 is not loading your form_validation.js
file and that's why it isn't running your initialization code in that file.
IE8 没有加载您的form_validation.js
文件,这就是为什么它没有在该文件中运行您的初始化代码。
You need to change the type on your script tag to type="text/javascript"
or remove the type entirely. You have type="application/javascript"
which is not correct.
您需要将脚本标记上的类型更改为type="text/javascript"
或完全删除该类型。你有type="application/javascript"
哪个不正确。
After further exploration, see Why doesn't IE8 recognize type="application/javascript" in a script tag?for more discussion.
经过进一步探索,请参阅为什么 IE8 不能识别脚本标记中的 type="application/javascript"?更多讨论。