沙滩星空的博客沙滩星空的博客

JS操作HTML DOM对象

1. Select 对象
如果 select 元素下的所有 option 元素均没有指定 selected 属性,会默认选中第一个。
可以通过 select.selectedIndex 获取到选中的 option 元素的索引。
可以通过 select.options[select.selectedIndex] 获取到选中的 option 元素。

获取选中的option元素对象

var selectedIndex = elemObj.selectedIndex;
var selectedEle = elemObj.options[selectedIndex];

获取 option 元素内的文本

var elemText = elemObj.text; 
var elemText = elemObj.innerHtml; 

获取option 元素的 value 属性值

var elemValue = elemObj.value;

2. 给元素添加或移除class类

document.getElementById("div").classList.add("类名");
document.getElementById("div").classList.add("类名1","类名2","类名3",...);
document.getElementById("div").classList.remove("类名");
document.getElementById("div").classList.remove("类名1","类名2","类名3",...);
divObj.classList.contains('类名'); //return true or false 检查是否含有某个CSS类

js获取当前select 元素值的代码 http://www.luyixian.cn/javascript_show_127157.aspx
HTML DOM Select 对象 https://www.w3school.com.cn/jsref/dom_obj_select.asp
JS之给元素添加类的方法 https://www.cnblogs.com/jessie-xian/p/11576367.html

HTML DOM 教程 https://www.runoob.com/htmldom/htmldom-tutorial.html
HTML DOM 对象之元素对象 https://www.runoob.com/jsref/dom-obj-all.html

未经允许不得转载:沙滩星空的博客 » JS操作HTML DOM对象

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址