1个DIV里有 id="box" 和1个按钮,目前多个DIV里 都有相同的box和按钮。
我想每一个按钮点击只能获取当前DIV里触发,显示不一样的id里文字字符串。
这个怎么实现?
<div>
<div id="box">我是box</div>
<a type="button" href="#" onclick="show()">点击</a>
</div>
<div>
<div id="box">我是box1</div>
<a type="button" href="#" onclick="show()">点击</a>
</div>
<div>
<div id="box">我是box2</div>
<a type="button" href="#" onclick="show()">点击</a>
</div>
<div>
<div id="box">我是box3</div>
<a type="button" href="#" onclick="show()">点击</a>
</div>
<div>
<div id="box">我是box4</div>
<a type="button" href="#" onclick="show()">点击</a>
</div>
<div id="foo"></div>
<script type="text/javascript">
function show() {
let box = $("#box").html();
$("#box").each(function(){
alert("触发!");
// 总是触发我是box
$("#foo").html(box);
});
}
</script>
不知道这是不是你要的效果
https://codesandbox.io/s/nost...