gpt4 book ai didi

javascript - 将html元素传递给javascript中的函数

转载 作者:太空宇宙 更新时间:2023-11-04 15:30:35 25 4
gpt4 key购买 nike

...这个问题已经被问过很多次了,但我仍然无法解决我的问题。

我有一个元素列表,动态创建,由类“box”定义,如下所示:

<div class="box">
<button onclick="buttonFunction(element.inputBox)"></button>
<input class="inputBox"></input>
</div>
<div class="box">
<button onclick="buttonFunction(element.inputBox)"></button>
<input class="inputBox"></input>
</div>
....
<div class="box">
<button onclick="buttonFunction(element.inputBox)"></button>
<input class="inputBox"></input>
</div>

然后我希望函数“buttonFunction(element)”如下所示:

function buttonFunction(element){
$('.element').show()
}

其中element是具体的inputbox到具体的box div。

最佳答案

只要您使用的是 jQuery,就容易多了:

HTML:

<div class="box">
<button></button>
<input class="inputBox"></input>
</div>
<div class="box">
<button></button>
<input class="inputBox"></input>
</div>
<div class="box">
<button></button>
<input class="inputBox"></input>
</div>

JS(把它放在 $(document).ready(...) 里:

$('.box button').click(function(){
$(this).next('.inputBox').show();
});

这是一个 example

关于javascript - 将html元素传递给javascript中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14488001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com