天天看點

IE6中 “無法設定selected屬性”的腳本錯誤提示

在IE6下,彈出腳本錯誤的提示:無法設定selected屬性 未指明的錯誤

代碼:selectObj.val(defaultSelect);//選中指定項

出現場景:動态填充select标簽:為其添加option選項,之後設定預設項時出現 後期測試:發現并不一定出現,非常怪異,應該是還沒有完全明白發生這個問題的時機

解決辦法:

在設定selected屬性的時候使用setTimeout函數設定一個任意時間即可 setTimeout(function(){      selectObj.val(defaultSelect);//選中指定項

},0);

IE 6 can throw an error "Error: Could not set the selected property. Unspecified error." when you try to manipulate select elements by adding options and then selecting one of your newly created items. I came across this while trying to use jQuery's .val() function to set the selected element after I had added some options to my select.

繼續閱讀