需要准备的材料分别有:电脑、浏览器、html编辑器。
1、首先,打开html编辑器,新建html文件,例如:index.html。
2、在index.html中的<script>标签,输入js代码:
var a = '通过id获取:' + $('#test').html()
var b = '通过标签名获取:' + $('div').html()
$('body').append('<br/>' + a)
$('body').append('<br/>' + b)
3、浏览器运行index.html页面,此时成功通过id和标签名称获取到了节点并打印了节点内容。
用JS获取Html标签属性有两种方法: 如Web开发 JS代码: var ka=document.getElementByI d("link")alert(ka.getAttribute("id"))alert(ka.id)两种方法都能d出a标签的ID属性,但从各浏览器兼容上来说用ka.id的方式更好,但对于自定义属性element.style.属性名。element.属性名。
例如:
<div class="aa" id="div" style="width:100pxheight:100px">用于测试的div</div>
<script>
var div=document.getElementById("div")
console.log(div)
console.log(div.style.width)
console.log(div.style.height)
console.log(div.id)
console.log(div.className)//注意这个,不是class是className
</script>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)