以下Javascript在IE8中不起作用,但在FF,Safari,IE9和Chrome中运行良好。我承认自己是一个使用Javascript的noobie,我花了2天时间寻找一个没有运气的解决方案。 IE8不喜欢切换功能或隐藏。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<style type="text/css">
.hide {
display: none;
}
</style>
<script type="text/javascript">
function toggleField(evt) {
evt = (evt) ? evt : event;
var target = (evt.target) ? evt.target : evt.srcElement;
var block = document.getElementById("specialty");
if (target.id == "fieldFlag1") {
block.style.display = "block";
} else {
block.style.display = "none";
}
}
</script>
</head>
<body>
<div id="profession">
Field of professional work (as applicable):
<select size="1" name="profession">
<option>-- Choose One --</option>
<option value="Architecture">Architecture</option>
<option value="FineArts">Arts, Fine</option>
<option value="PerformingArts">Arts, Performing</option>
<option value="Business">Business</option>
<option value="Consulting">Consulting</option>
<option value="Counseling">Counseling</option>
<option value="Dentistry">Dentistry</option>
<option value="Education">Education</option>
<option value="Engineering">Engineering</option>
<option value="Environment">Environment</option>
<option value="Finance">Finance</option>
<option value="Government">Government</option>
<option value="Healthcare—NonMedical">Healthcare—Non Medical</option>
<option value="HigherEducation">Higher Education</option>
<option value="InformationTechnology">Information Technology</option>
<option value="Law">Law</option>
<option value="Marketing">Marketing</option>
<option value="Media/Communications">Media/Communications</option>
<option value="Medicine">Medicine</option>
<option value="Military">Military</option>
<option value="Music">Music</option>
<option value="NonProfit">Non Profit</option>
<option value="Nursing" id="fieldFlag1" onclick="toggleField(event)">Nursing</option>
<option value="PublicService">Public Service</option>
<option value="Science/Research">Science/Research</option>
<option value="SocialServices">Social Services</option>
<option value="Sports">Sports</option>
<option value="Trade/Craft">Trade/Craft</option>
</select>
</div>
<div id="specialty" class="hide">
Choose Specialty:
<select name="Specialty" size="1">
<option value="nursing">Nursing</option>
<option value="nurse Practitioner Unspecified">Nurse·Practitioner·Unspecified</option>
<option value="nurse inactive">Nurse·Inactive</option>
<option value="womens health">Womens·Health</option>
<option value="administration">Administration</option>
<option value="hospital">Hospital</option>
<option value="critical care">Critical·Care</option>
<option value="home health">Home·Health</option>
<option value="surgical">Surgical</option>
<option value="clinical Research">Clinical·Research</option>
<option value="family health">Family·Health</option>
<option value="acute care nursing">Acute·Care·Nursing</option>
<option value="community Health Nursing">Community·Health·Nursing</option>
<option value="emergency nursing">Emergency·Nursing</option>
<option value="nurse practitioner: adult health">Nurse·Practitioner:·Adult·Health</option>
<option value="nurse practitioner: pediatrics">Nurse·Practitioner:·Pediatrics</option>
<option value="nurse practitioner: neonatal">Nurse·Practitioner:·Neonatal</option>
<option value="nurse practitioner: acute care">Nurse·Practitioner:·Acute·Care</option>
<option value="nurse practitioner: family">Nurse·Practitioner:·Family</option>
<option value="nurse practitioner: gerontology">Nurse·Practitioner:·Gerontology</option>
<option value="nurse practitioner: women's health">Nurse·Practitioner:·Women's·Health</option>
</select>
</div>
</body>
</html>
答案 0 :(得分:0)
为了使您的代码在IE8中运行,您需要更改:
var target = (evt.target) ? evt.target : evt.srcElement;
使用:
var target = (evt.currentTarget) ? evt.currentTarget : event.srcElement;
原因是9.0之前的Internet Explorer版本不以标准方式处理事件。
每当触发事件时,名为event
的全局变量随处可用。它被称为事件对象。
您可以查看此网址以获取更多详细信息:
http://msdn.microsoft.com/en-us/library/ie/ms535863%28v=vs.85%29.aspx
答案 1 :(得分:0)
function toggleField(target) {
var block = document.getElementById("specialty");
if (target.options[target.selectedIndex].value == "Nursing") {
block.style.display = "block";
} else {
block.style.display = "none";
}
}
并使用HTML(而非onchange
使用select
的事件代替option
的{{1}}):
onclick
其中<select size="1" name="profession" onchange="toggleField(this)">
指向已更改的元素。只是不写所有那些
this
关于选项的 evt = (evt) ? evt : event;
var target = (evt.target) ? evt.target : evt.srcElement;
- 我认为它根本不可用。
UPD: Here是一个答案,您可以在其中找到不支持onclick
的onclick事件的浏览器列表
答案 2 :(得分:0)
首次选择更改
var e = document.getElementById("your First Select");
var valueOption= e.options[e.selectedIndex].value;
var block = document.getElementById("specialty");
if (valueOption == "Nursing")
block.style.display = "block";
} else {
block.style.display = "none";
}