js获取被点击元素:
可参见原demo:https://github.com/liyuanhong/learn-js-css-html5/tree/master/html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>获取被点击的元素</title>
</head>
<style type="text/css">
body {margin: 0px;}
#container {width: 700px;height: 300px;padding:5px;background-color: #edfdef;}
</style>
<script src="../lib/jquery/jquery/dist/jquery.min.js"></script>
<script type="text/javascript">
function getClickEle(ele){
console.log($(ele).val()); //方法一:通过jquery对象,获取被点击的元素
console.log(ele.tagName); //方法二:通过传入js 元素对象,获取
//$(document).click(function(e){console.log(e.target.value)});
}
function getClildVal(ele){
console.log($(ele).find("#girl_1")[0].tagName); //方法五:获取被点击的元素的子元素转jquery对象,再获取子元素
console.log($($(ele).find("#girl_1")[0]).attr("id")); //方法六:获取被点击的元素的子元素
}
</script>
<body>
<div id="container">
<ul id="ul_1">
<li id="ul1_li1">
<label>姓名:</label><input type="text" style="width:100px;" value="Mr li">
</li>
<li id="ul1_li1">
<label>电话:</label><input type="checkbox" style="width:100px;" value="1111111111" οnclick="getClickEle(this)">
<label>手机:</label><input type="checkbox" style="width:100px;" value="2222222222" id="phone">
<label>email:</label><input type="checkbox" style="width:100px;" value="333@333">
<label>QQ:</label><input type="checkbox" style="width:100px;" value="5555555">
</li>
<li>
<div style="width:200px;height:200px;background-color:gray;" οnclick="getClildVal(this)">
<p>下面有个可爱的女孩:</p>
<label><img style="height:150px;" src="../pic/e2a508ea22d6f7fd138b4458ecce0350.jpg" id="girl_1"></label>
</div>
</li>
</ul>
</div>
</body>
<script type="text/javascript">
$("#phone").click(function(){
console.log($(this).val()); //方法三:获取被点击的元素,转jquery对象
console.log(this.tagName); //方法四:获取被点击的元素
});
</script>
</html>
参见以上代码