深圳网站建设 (www.innont.com )作为市场资深品牌,8年来,立足广东,面向全国,已服务过3000多家具有顶级发展潜力的企业,并一直保持良好的合作伙伴关系,成为中国第一高端精品网站设计策划机构,网站建设第一品牌!
在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.
写了个jQuery连动三级下拉的插件, 走的是ajax异步路线,(正在整理), 可在IE6里老是报 "无法设置selected属性, 无效索引"的错, 但在别的浏览器 经研究找到了解决方法. 造成这个原因可能是你的select里的内容是动态输出的, 那么可加个延迟处理就完美了.如:
by:
$("#select").html("<option value=1>选项1</option><option value=2>选项
2</option>");
setTimeout(function () {
$("#select").val(2);
},10);这里延迟的是10ms, 经测试发现延迟0ms也可.这可能是ie6一个独特的bug吧.
本文由深圳网站设计公司:创新互联整理,转载时请保留此链接,谢谢合作!