我試過這個.toggle()和下面的更長的表單。如果我取出.replaceWith的行,它可以很好地顯示和隱藏#表單,但是它可以......#表單取消隱藏和#show-form更改正確,但不適用於下次點擊。無法得到.replaceWith()正常工作
任何人都可以看到我在做什麼錯?
<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('隱藏窗體'),反之亦然 –
實際上,喬丹有最好的答案。 – bozdoz
@Joardan ...剛試過你的建議,它的工作原理!你爲什麼不把它移到答案,以便我可以正確地稱讚你? –