
1、首先,打开html编辑器,新建html文件,例如:index.html,编写问题基础代码。
2、在index.html中的<script>标签,输入js代码:$('li a').each(function (i, e) {$(this).attr('id', 'id1_' + i)})。
3、浏览器运行index.html页面,此时发现元素被js按照需求新增了id属性。
因为是用class,没有id,所以只能假设只有一个div是这个class来做:
<!DOCTYPE html><html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="div-test">123</div>
<script>
document.getElementsByClassName("div-test")[0].style.backgroundColor = "red"
</script>
</body>
</html>
望采纳,谢谢
引用楼上的document.getElementById("01").className="aa"//aa是class名称如果用jquery的方法,就更简单一点$("#01").attr("class","aa")
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)