一个元素中的onclick在javascript中显示另一个元素 [英] onclick in one element show another element in javascript

查看:100
本文介绍了一个元素中的onclick在javascript中显示另一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想单击加号,然后在每一步中显示下一个字段,但是我的代码在第三步中不起作用.是否有任何方法可以无限制地执行这些步骤?tnx

i would like to click in plus ,then show the next field in every step but my code does not work in third step .is there any method to do these steps for unlimited time?tnx

<html>
<head>
<script>
function showfield(){
document.getElementById("hiddenfield").style.display="block";
}
</script>
</head>

<body>
<div class="form-group" >
<div class="rightbox"> <label for='phone'>Phone1</label></div>
<input type="tel" value="" />
<div class="plus" onClick="showfield()">+</div>
</div>

<div class="form-group" style="display:none;" id="hiddenfield">
<div class="rightbox"><label for='phone'>Phone2</label></div>
 <input type="tel" value="" />
<div class="plus" onClick="showfield()">+</div></div>
</div>

<div class="form-group" style="display:none;" id="hiddenfield">
<div class="rightbox"><label for='phone'>Phone3</label></div>
 <input type="tel" value="" />
<div class="plus" onClick="showfield()">+</div></div>
</div>
</body>
</html>

推荐答案

您可以为每个隐藏字段使用唯一的ID,然后将当前ID作为参数发送给函数.

You can use unique id for each hidden field, then send current id to your function as parameter.

function showfield(id) {
     document.getElementById(id).style.display="block";
}

另一个选择是jquery:

Another option is jquery:

$('.plus').on('click', function() {
    $(this).parent().next().show();
});

查看 jsfiddle .

这篇关于一个元素中的onclick在javascript中显示另一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆