在網頁頭部定義js代碼javascript
<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方法未定義,不兼容問題谷歌、火狐正常。java
通過考慮以後把代碼定義在 <BODY>裏,打開頁面默認加載js方法app
所有兼容了ide
<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>