IE瀏覽器select屬性onchange遇到的坑
阿新 • • 發佈:2018-01-10
代碼 ntb 頁面 html d+ color ide 全部 ava
在網頁頭部定義js代碼
<script type="text/javascript"> function gradeChange(e){ var selectId = document.getElementById("selectId"); var index = selectId.selectedIndex; var value = selectId.options[index].value; var id = selectId.options[index].id; document.getElementById(id).setAttribute(‘disabled‘,‘disabled‘); var ul = document.getElementById("ul"); var li = document.createElement("li"); li.setAttribute(‘id‘,‘li_‘+id); li.setAttribute(‘onclick‘,‘del(‘+id+‘)‘); var span = document.createElement("span"); span.innerHTML=value+‘ב; li.appendChild(span); ul.appendChild(li); selectId[0].selected = true; var hide = document.getElementById("hotTagName"); if(hide.value==‘‘ || hide.value=="null"){ hide.setAttribute(‘value‘,value); }else{ hide.setAttribute(‘value‘,hide.value+‘,‘+value); } } </script>
<body>
<select style="float:left;" id="selectId" onchange="javascript:gradeChange(this);"> <option value="">--熱點標簽--</option> <logic:notEmpty name="tagList"> <logic:iterate id="tag" name="tagList" indexId="i"> <option id="id_${i}" value ="${tag.name}">${tag.name}</option> </logic:iterate> </logic:notEmpty> </select>
<body>
IE、360報錯gradeChange方法未定義,不兼容問題谷歌、火狐正常。
經過考慮之後把代碼定義在 <BODY>裏,打開頁面默認加載js方法
全部兼容了
<body>
<select style="float:left;" id="selectId" onchange="javascript:gradeChange(this);"> <option value="">--熱點標簽--</option> <logic:notEmpty name="tagList"> <logic:iterate id="tag" name="tagList" indexId="i"> <option id="id_${i}" value ="${tag.name}">${tag.name}</option> </logic:iterate> </logic:notEmpty> </select>
<script> function gradeChange(e){ var selectId = document.getElementById("selectId"); var index = selectId.selectedIndex; var value = selectId.options[index].value; var id = selectId.options[index].id; document.getElementById(id).setAttribute(‘disabled‘,‘disabled‘); var ul = document.getElementById("ul"); var li = document.createElement("li"); li.setAttribute(‘id‘,‘li_‘+id); li.setAttribute(‘onclick‘,‘del(‘+id+‘)‘); var span = document.createElement("span"); span.innerHTML =value+‘ב; li.appendChild(span); ul.appendChild(li); selectId[0].selected = true; var hide = document.getElementById("hotTagName"); if(hide.value==‘‘ || hide.value=="null"){ hide.setAttribute(‘value‘,","+value); }else{ hide.setAttribute(‘value‘,hide.value+‘,‘+value); } } </script>
</body>
IE瀏覽器select屬性onchange遇到的坑