js点击当前元素传入id从而获取其他元素

点击一个元素,在点击函数里传入其他元素的id,可以拿到其他元素

若函数里传入this,则拿到的是当前被点击的元素本身

<div onclick="getother(one)">获取1号</div>  //点击时传入id one,获取id为one的元素
<div onclick="getother(two)">获取2号</div>
<div onclick="getother(this)">获取自己</div>  //点击时传入this,获取元素本身

<div id="one">我是1号</div>
<div id="two">我是2号</div>

<script>
    function getother(elem){
        console.log(elem)
    }
</script>

猜你喜欢

转载自www.cnblogs.com/huihuihero/p/12089979.html