[DOM]-4.setAttribute-增加標籤屬性
1.setAttribute來修改a元素的href
或者使用innerHTML來取出資料
condepen2
condepen3
<h1 class="titleClass"><a href="#">title</a></h1>
var el = document.querySelector('.titleClass a');
el.setAttribute('href','http://www.google.com.tw');
2.setAttribute來修改div元素的Id
<div class="str">hello</div>
#strId{
color: blue;
font-size: 48px;
}
var el =document.querySelector('.str');
el.setAttribute('id','strId');
3.getAttribute來取出a元素的href的值,getAttribute可改成textContent來取出文字內容。或者使用innerHTML來取出資料
<h1 class="titleClass"><a href="#">title</a></h1>
var el = document.querySelector('.titleClass a').getAttribute('href');
console.log(el);
condepen1
condepen2
condepen3
评论