element对象方法:
element.setAttribute(“属性名”,“属性值”)//添加
element.removeAttribute()//移除
<style>
.square{
width: 100px;
height: 100px;
background: red;
}
</style>
<script>
div.setAttribute("class","square");
div.removeAttribute("class","square");
</script>
select, option对象:
<body>
<select id="grade" style="width: 50px;height: 100px;">
<option value="1">一年级</option>
<option value="2">二年级</option>
<option value="3">三年级</option>
<option value="4">四年级</option>
</select>
<input type="button" onclick="grades()" value="确认"/>
<script>
function grades(){
var select=document.getElementById("grade");
var options=select.options;
console.log(options[select.selectedIndex].value);
var select=document.getElementById("grades");
console.log(select.length); //输出select长度
console.log(select.selectedIndex);
for (var i=0;i options.length;i++) {
console.log(options[i].value); //获取option内的value
console.log(options[i].selected); //输出true或false以判断是否被选中
console.log(options[i].text); //获取option内的内容(文本)
}
var option = new Option("五年级",5);
select.add(option); //添加一个option项目
select.remove(3); //移除一个option项目
}
</script>
</body>
window对象:
<input type="button" value="确认" onclick="trans()"/>
<script>
function trans(){
window.location.href="https://www.baidu.com";//window可以不要
alert("窗口");
confirm("确认退出吗");//返回值为真或假
console.log(document.domain);//
}
</script>