1. 获取标签值
可以使用对象的innerText属性获取标签的值。
<style type="text/css">
.attr{border:1px solid red;width:40px;height:20px;margin:30px 40px;}
</style>
<div name="gary" age="30" class="attr" onclick="onBtn(this)">按钮</div>
<script type="text/javascript">
function onBtn(obj){
alert(obj.innerText);
};
</script>
2. 获取标签属性
使用getAttribute方法获取标签的属性值:
<style type="text/css">
.attr{border:1px solid red;width:40px;height:20px;margin:30px 40px;}
</style>
<div name="gary" age="30" class="attr" onclick="onBtn(this)">按钮</div>
<script type="text/javascript">
function onBtn(obj){
alert(obj.getAttribute("name"));
alert(obj.getAttribute("age"));
alert(obj.getAttribute("class")); };
</script>