JavaScript-DOM进阶

目录

DOM进阶

对象属性

obj.attr

对象方法

obj.getAttribute("attr")

obj.setAttribute("attr","值")

obj.removeAtrribute("attr")

obj.hasAttribute("attr")

CSS属性操作

获取css属性值

getComputedStyle(obj).attr

设置css属性值

style对象

cssText()方法(不常用,一般给元素加一个class属性)

DOM遍历(获取元素的补充)

查找父元素obj.parentNode

查找子元素children[ ]firstElementChildlastElementChild;

查找兄弟元素previousElementSiblingnextElementSibling

innerHTML元素内部所有内容innerText元素内部的文本内容


DOM进阶

HTML属性操作

  1. 对象属性
  2. 对象方法

对象属性

obj.attr

如:oBtn.id

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>获取文本框、单选框、下拉列表的值</title>
	</head>
	<script type="text/javascript">
		window.onload=function(){
			var oBtn = document.getElementById("btn");
			oBtn.onclick=function(){
				var oTxt = document.getElementById("txt");
				alert(oTxt.value);
			}
		}
	</script>
	<script>
		window.onload=function(){
			var oBtn = document.getElementById("btn");
			var oFruit = document.getElementsByName("fruit");
			oBtn.onclick=function(){
				for(var i=0;i<oFruit.length;i++)
				{
					if(oFruit[i].checked)
					{
						alert(oFruit[i].value);
					}
				}
			}
			
		}
	</script>
	<script>
		window.onload=function(){
			var oBtn = document.getElementById("btn");
			var oSelect = document.getElementsByName("select")[0];
			oBtn.onclick=function(){
				alert(oSelect.value);
				
			}
		}
	</script>
	<body>
		<input type="text" id="txt" /><input type="button" id="btn" value="获取"/>
		
		<div>
			<label><input type="radio" name="fruit"  value="苹果" checked/>苹果</label>
			<label><input type="radio" name="fruit"  value="香蕉" />香蕉</label>
			<label><input type="radio" name="fruit"  value="西瓜" />西瓜</label>
			<input type="button"  id="btn" value="获取" />
		</div>
		
		<select name="select" >
			<option value="北京">北京</option>
			<option value="广东">广东</option>
			<option value="上海">上海</option>
			<option value="深圳">深圳</option>
			<option value="杭州">杭州</option>
		</select>
		<input type="button"  id="btn" value="获取" />
		
	</body>
</html>

对象方法

  1. obj.getAttribute("attr")

  2. obj.setAttribute("attr","值")

  3. obj.removeAtrribute("attr") class

  4. obj.hasAttribute("attr")

1.obj.attr = obj.getAttribute( )

自定义属性用obj.attr(对象属性)无法实现,只能用(对象方法)getAttribute()实现

oBtn.onclick=function(){
	alert(oBtn.id);
	alert(oBtn.getAttribute("id"));
	}

2.obj.attr=“值”  =  obj.setAttribute(“attr”,“值”)

设置某个属性的值

    var oInput = document.createElement("input");
    oInput.id="submit";
    oInput.setAttribute("id","submit");

3.obj.removeAttribute("attr")

oP[0].removeAttribute("class");

4.obj.hasAttribute("attr")

返回布尔值

CSS属性操作

  1. 获取css属性值
  2. 设置css属性值

获取css属性值

getComputedStyle(obj).attr

obj表示DOM对象,attr表示css属性名

属性名使用的是“骆驼峰型”
font-sizefontSize
border-bottom-widthborderBottomWidth
background-colorbackgroundColor

 

 

 

 

 

oBtn.id = oBtn["id"]

getComputedStyle(oBox).backgroundColor
等价于
getComputedStyle(oBox)["backgroundColor"]

设置css属性值

style对象

obj.style.attr=“值”;

attr采用骆驼峰型,即为一个变量,变量不允许出现-

oBtn.style.width = "100px"

cssText()方法(不常用,一般给元素加一个class属性

obj.style.cssText = "字符串"

oDiv.style.cssText="wdith:100px;height:100px;border:1px solid gray;";

为一个元素同时设置多个CSS属性很少使用cssText()来实现,更倾向于使用操作HTML属性的方式加上一个class属性obj.className=class名

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<style type="text/css">
		.oldBox{
			width:100px;
			height:100px;
			background-color:hotpink;
		}
		.newBox{
			width:50px;
			height: 50px;
			background-color: lightskyblue;
		}
	</style>
	<script type="text/javascript">
		window.onload=function(){
			var oBtn = document.getElementById("btn");
			var oBox = document.getElementById("box");
			var flag = true;
			oBtn.onclick=function(){
				if(flag)
				{
					oBox.className="newBox";
					flag = false;
				}
				else{
					oBox.className="oldBox";
					flag = true;
				}
				
			}
		}
	</script>
	<body>
		<input type="button" id="btn" value="切换" />
		<div id="box" class="oldBox">
			
		</div>
	</body>
</html>

DOM遍历(获取元素的补充)

查找父元素
obj.parentNode

var oParent = this.parentNode

查找子元素
children[ ]
firstElementChild
lastElementChild;

oUl.removeChild(oUl.lastElementChild)

查找兄弟元素
previousElementSibling
nextElementSibling

var preElement =  oUl.children[2].previousElementSibling;

innerHTML
元素内部所有内容
innerText
元素内部的文本内容

字符串换行\

body.innerHTML='<span>HTML</span>\
                <span>JavaScript</span>\
                <span>入门教程</span>';

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值