jquery show hide替换区域
我如何使用jQuery显示/隐藏相同的div区域…基本上交换内容?
这是我到目前为止,我无法弄清楚如何隐藏以前的内容并将其替换为新的内容:
$(document).ready(function(){ $('.content').hide(); $('a').click(function(){ $('.content').show('slow'); }); //then hide previous content }); A B content here content here
如果你将内容包装在div中,这会更容易一些,如下所示:
content here content here
这在jQuery中:
$('a').click(function(){ $("#wrap > div").hide(); //hide previous $('.content').show('slow'); //show what's clicked on });
既然你有一个带有你的ID的约定,你可以使用它,或者给你链接一个类,或者包装它们,如下所示:
content here content here
然后,您可以为所有链接使用单个事件处理程序,如下所示:
$('#wap > div:not(.start-content)').hide(); //Initial hide $("#links a").click(function() { $("#wrap > div").hide(); //hide previous var id = $(this).attr("id").replace('-link', ''); //matching id $('#' + id).show('slow'); //show what's clicked on });
保持内容div可见; 相反,只需用$('.content').html(strvalue);
换掉HTML内容$('.content').html(strvalue);