2

以下 Javascript 在 IE8 中无法运行,但在 FF、Safari、IE9 和 Chrome 中运行良好。我承认自己是 Javascript 的菜鸟,我花了 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&#8212;Non&#160;Medical</option>
                <option value="HigherEducation">Higher&#160;Education</option>
                <option value="InformationTechnology">Information&#160;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&#160;Profit</option>
                <option value="Nursing" id="fieldFlag1" onclick="toggleField(event)">Nursing</option>
                <option value="PublicService">Public&#160;Service</option>
                <option value="Science/Research">Science/Research</option>
                <option value="SocialServices">Social&#160;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>
4

3 回答 3

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";
}
于 2013-01-15T18:23:23.263 回答
0

为了使您的代码在 IE8 中工作,您需要更改:

var target = (evt.target) ? evt.target : evt.srcElement;

和:

var target = (evt.currentTarget) ? evt.currentTarget : event.srcElement;

原因是 Internet Explorer 9.0 之前的版本不以标准方式处理事件。

每当触发一个事件时,一个名为的全局变量event在任何地方都可用。它被称为事件对象。

您可以查看此 URL 以获取更多详细信息:

http://msdn.microsoft.com/en-us/library/ie/ms535863%28v=vs.85%29.aspx

于 2013-01-15T18:17:26.337 回答
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's onclick):

<select size="1" name="profession" onchange="toggleField(this)">

wherethis指向一个被改变的元素。只是为了不写所有这些

evt = (evt) ? evt : event;
var target = (evt.target) ? evt.target : evt.srcElement;

onclick关于选项 - 我认为它根本不应该可用。

UPD: 是一个答案,您可以在其中找到不支持 onclick 事件的浏览器列表<option>

于 2013-01-15T18:20:40.930 回答