js中option操作

 

<html>   
<head>   
<script language="javascript">
var infoMap  = new Object() ;
function number(){   
var obj = document.getElementById("mySelect");
    //obj.options[obj.selectedIndex] = new Option("我的吃吃","4");  
    //在当前选中的那个的值中改变   
    //obj.options.style.cssText =("background:red");
    obj.options.add(new Option("我的吃吃","4"));//再添加一个option
    //alert(obj.length);
    obj.options[obj.length-1].style.cssText =("background:#66FF33");
    /*
    for (var i = 0; i < obj.length; i++) {
    obj.options.add(new Option(obj[i], obj[i]));
    }
    */
    //alert(obj.selectedIndex);//显示序号,option自己设置的
    /*
    var name = obj.options[obj.selectedIndex].text;
    infoMap[name+'-1'] = {
        name : name,
        age : '1'
    };
    if( infoMap[name+'-1'] ) {
        var oldname = infoMap[name+'-1'].name;
        infoMap[name+'-1'].name = oldname+','+name;
    }
    alert(infoMap[name+'-1'].age);
    */
    //obj.options[obj.selectedIndex].text = "我的吃吃";更改值   
   //obj.remove(obj.selectedIndex);//删除功能   
}   
</script>   
</head>   
<body>   
<select id="mySelect" multiple>   
     <option title='hello' selected>我的包包</option>   
     <option style="background:red" value="#333333">我的本本</option>   
     <option>我的油油</option>   
     <option>我的担子</option>   
</select>
</p>
<p style="color:#fff;background:#780e1a;">Test for set style.</p>
<p><button onclick="document.getElementsByTagName('p')[0].style='color:#000;background:#eee;'">set style(op only)</button></p>
<p><button onclick="document.getElementsByTagName('p')[0].style.cssText='background:red;'">set style(ff & ie)</button></p>
<p><button onclick="document.getElementsByTagName('p')[0].setAttribute('style','color:#000;background:#eee;')">set style(ff & op)</button></p>
<p><button onclick="document.getElementsByTagName('p')[0].getAttribute('style').setAttribute('cssText','color:#000;background:#eee;')">set style(ie only)</button></p>
<p>
<input type="button" name="button" value="查看结果" onclick="number();">   
</body>   
</html>
 

 

 

你可能感兴趣的:(JavaScript,html,IE)