JS 获取html标签属性和设置标签样式

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.font16 {font-size:16px;}
</style>
</head>

<body>

<h1>我是标题1 h1</h1>
<button onclick="h1()">获取h1标签内容</button>
<h2 id="h2">我是标题2 h2</h2>
<button onclick="h2()">获取h2标签内容</button>
<h3 class="h3">我是标题3 h3</h3>
<button onclick="h3()">获取h3标签内容</button>
<h4 class="h3">我是标题4 h4</h4>
<h5><span>我是标题5</span> h5</h5>
<button onclick="h5()">获取h5标签内容</button>
<h6 class="h6">我是标题6 h6</h6>
<button onclick="h6()">设置h6标签样式</button>

<script type="text/javascript">
function h1(){
  var h1 = document.getElementsByTagName('h1')[0];
  console.log(h1.innerHTML);
}
function h2(){
  var h2 = document.getElementById('h2');
  console.log(h2.innerHTML);
}
function h3(){
  //var h3 = document.getElementsByClassName('h3')[0];
  //console.log(h3.innerHTML);
  var h3 = document.getElementsByClassName('h3');
  for(var i=0;i<h3.length;i++){
    console.log(h3[i].innerHTML);
  }
}
function h5(){
  var h5 = document.getElementsByTagName('h5')[0];
  var h5span = h5.getElementsByTagName('span')[0];
  console.log(h5span.innerHTML);
}
function h6(){
  var h6 = document.getElementsByClassName('h6')[0];
  h6.style.color = 'red';
  h6.className = 'h6 font16';
}

//可见区域高度
var js_height = document.documentElement.clientHeight;
var jquery_height = $(window).height();
console.log(js_height+','+jquery_height);
</script>

</body>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值