我有一个以锚标记为边界的文本。用户单击它后,它应该在两个文本之间切换。
我了解如何在JQuery中切换按钮,但不知道如何使用链接实现它。
<h1>Questions and Answers</h1>
<p>First Question: What is the capital of Germany?
<br>
<i>Click <b><a id="toggle_this"></a>Here</a></b> to show/hide answer!</i>
</p>
<p>Answer: Berlin.</p>
<script>
$(document).ready(function() {
$("toggle_this").click(function() {
$(this).hide();
});
});
</script>
首先更改id
为class="toggle_this"
。然后使用选择器.toggle_this
。
然后您可以使用e.preventDefault()
该链接停止默认操作。然后使用closest()
和next()
您可以得到答案<p>
,然后toggle()
它。
$(".toggle_this").click(function (e) {
e.preventDefault();
$(this).closest('p').next().toggle();
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句