方法有两种。
第一种通过
< select id = "sel" > < option value = "1" >1</ option > < option value = "2" selected = "selected" >2</ option > < option value = "3" >3</ option > </ select >
第二种为通过前端js来控制选中的项:
< script type = "text/javascript" >
function change(){
document.getElementById("sel")[2].selected=true;
}
</ script >
< select id = "sel" >
< option value = "1" >1</ option >
< option value = "2" >2</ option >
< option value = "3" >3</ option >
</ select >
< input type = "button" value = "修改" onclick = "change()" />获取
var val = document.all.Item.options[document.all.Item.selectedIndex].text var i=document.getElementById( 'sel' ).options[document.getElementById( 'sel' ).selectedIndex].value;
一些其它操作
立即学习“前端免费学习笔记(深入)”;
1)动态创建select
function createSelect(){
var mySelect = document.createElement( "select" );
mySelect.id = "mySelect" ;
document.body.appendChild(mySelect);
}2)添加选项option
传媒企业网站系统使用热腾CMS(RTCMS),根据网站板块定制的栏目,如果修改栏目,需要修改模板相应的标签。站点内容均可在后台网站基本设置中添加。全站可生成HTML,安装默认动态浏览。并可以独立设置SEO标题、关键字、描述信息。源码包中带有少量测试数据,安装时可选择演示安装或全新安装。如果全新安装,后台内容充实后,首页才能完全显示出来。(全新安装后可以删除演示数据用到的图片,目录在https://
0
function addOption(){
//根据id查找对象,
var obj=document.getElementById( 'mySelect' );
//添加一个选项
obj.add( new Option( "文本" , "值" ));
}3)删除所有选项option
function removeAll(){
var obj=document.getElementById( 'mySelect' );
obj.options.length=0;
}4)删除一个选项option
function removeOne(){
var obj=document.getElementById( 'mySelect' );
//index,要删除选项的序号,这里取当前选中选项的序号
var index=obj.selectedIndex;
obj.options.remove(index);
}5)获得选项option的值
var obj=document.getElementById( 'mySelect' ); var index=obj.selectedIndex; //序号,取当前选中选项的序号 var val = obj.options[index].value;
6)获得选项option的文本
var obj=document.getElementById( 'mySelect' ); var index=obj.selectedIndex; //序号,取当前选中选项的序号 var val = obj.options[index].text;
7)修改选项option
var obj=document.getElementById( 'mySelect' ); var index=obj.selectedIndex; //序号,取当前选中选项的序号 var val = obj.options[index]= new Option( "新文本" , "新值" );
8)删除select
function removeSelect(){
var mySelect = document.getElementById( "mySelect" );
mySelect.parentNode.removeChild(mySelect);
}以上就是html中关于标签如何设置默认选中的选项详解的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号