我已经尝试过使用.toggle()和下面更长的形式。如果我删除.replaceWith的行,则可以很好地显示和隐藏#the-form,但是将其放入...#the-form可以取消隐藏,并且#show-form可以正确更改,但是对于下一次单击均无效。
谁能看到我在做什么错?
<script type="text/javascript">
jQuery('#show-form').click(
function() {
if (jQuery("#the-form").is(":hidden")) {
jQuery('#the-form').show('fast');
jQuery('#show-form').replaceWith('<div id="show-form">Hide Form</div>');
} else {
jQuery('#the-form').hide('fast');
jQuery('#show-form').replaceWith('<div id="show-form">Show Form</div>');
}
});
</script>
更新:删除引号中的反斜杠。
UPDATE 2工作版本:
<script type="text/javascript">
jQuery('#show-form').live('click',
function() {
if (jQuery("#the-form").is(":hidden")) {
jQuery('#show-form').html('Hide Form');
jQuery('#the-form').show('fast');
} else {
jQuery('#show-form').html('Show Form');
jQuery('#the-form').hide('fast');
}
});
谢谢您的帮助!
最佳答案
回答:
为什么不只使用html函数? jQuery('#show-form')。html('Hide Form'),反之亦然
关于javascript - 无法让.replaceWith()正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8190922/