我建立一个“编辑个人资料”页。隐藏/显示子元素的onclick
这是我想做的事:
- 在每一部分,用人单位将显示和编辑表单将被隐藏。
- 当我点击“编辑雇主”按钮,编辑表单将显示与用人单位将被隐藏。
这里是我做过什么使用jQuery。当我点击“编辑雇主”按钮时,它不起作用。我不知道为什么这不起作用。
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<div class="edit">
<form class="editForm">
employer: <input type="text" value="Citigroup" />
</form>
<div class="contents">Employer: Citigroup</div>
<button class="editButton">Edit Employer</button>
</div>
<script>
$('div.edit').each(function(i) {
$(this).children('.editForm').hide();
})
$('div.edit').each(function() {
$(this).children('.editButton').click(function() {
$(this).children('.editForm').show();
$(this).children('.contents').hide();
});
})
</script>
</body>
</html>
你的'this'内的点击处理程序不同于每个。在点击中,它指的是.editButton元素。 – Axnyff
'.editButton'没有孩子。事件处理程序中的$(this)'指的是被点击的按钮。使用'$(this).siblings('。editForm')。show()。siblings('。contents).hide()' – Tushar
欢迎。避免使用'each',jQuery为你做。 '$('div.edit .editForm')。hide();'或者更好,使用CSS来隐藏元素。并绑定事件'$('div.edit .editButton')。click(function(){...});' – Tushar