Javascript 显示/隐藏jQuery函数不工作
我正在尝试使用jQuery编写一个简单的显示/隐藏div。基本上,当我点击Javascript 显示/隐藏jQuery函数不工作,javascript,jquery,show-hide,Javascript,Jquery,Show Hide,我正在尝试使用jQuery编写一个简单的显示/隐藏div。基本上,当我点击.artist ken时,我希望艺术家主页div消失,并用.ken gallery替换它 到目前为止,我有这个,但除了跳到页面顶部之外,它什么也没做: $('.artist-ken').click(function(){ $('.artists-home').hide().show('ken-gallery'); }); jQuery.show()不将选择器作为第一个参数,请尝试以下操作: $('.artist-
.artist ken
时,我希望艺术家主页
div消失,并用.ken gallery
替换它
到目前为止,我有这个,但除了跳到页面顶部之外,它什么也没做:
$('.artist-ken').click(function(){
$('.artists-home').hide().show('ken-gallery');
});
jQuery.show()
不将选择器作为第一个参数,请尝试以下操作:
$('.artist-ken').click(function(){
$('.artists-home').hide();
$('.ken-gallery').show();
});
我假设您要隐藏的元素具有类“.ken gallery”,并且您要显示的元素具有类“.artists home”jQuery.show()
不将选择器作为第一个参数,请尝试以下操作:
$('.artist-ken').click(function(){
$('.artists-home').hide();
$('.ken-gallery').show();
});
我假设您要隐藏的元素具有类“.ken gallery”,并且您要显示的元素具有类“.artists home”尝试以下操作:
$('.artist-ken').click(function(e){
e.preventDefault();
$('.artists-home').hide();
$('.ken-gallery').show();
});
函数preventDefault()
将停止在页面中跳转。您需要单独的show
来显示另一个div。另外
在ken gallery
中丢失了请尝试以下操作:
$('.artist-ken').click(function(e){
e.preventDefault();
$('.artists-home').hide();
$('.ken-gallery').show();
});
$('.artist-ken').click(function(e){
e.preventDefault();
$(".artists-home").hide( 0, function() {
$('.ken-gallery').show();
});
});
函数preventDefault()
将停止在页面中跳转。您需要单独的show
来显示另一个div。另外
在ken gallery
中丢失了试试这个
$('.artist-ken').click(function(e){
e.preventDefault();
$(".artists-home").hide( 0, function() {
$('.ken-gallery').show();
});
});
$('.artist-ken').click(function(e){
e.preventDefault();
$('.artists-home').hide();
$('.ken-gallery').show();
});
您所做的不是show()
函数的实际参数。另外,即使是这样,您也没有指定它是一个类。它只能使用函数、持续时间、nothing或对象引用您还可以创建一个单向侦听器来显示另一个或隐藏另一个
$(document).click(function(e){
if( e.target.classList.contains('artists-home') ) {
e.preventDefault();
$(e.target).hide();
$('.ken-gallery').show();
}else if( e.target.classList.contains('ken-gallery') ){
e.preventDefault();
$(e.target).hide();
$('.artists-home').show();
}
});
Chonger还说,如果您想要一个淡入淡出,这是jQuery的任何显示/隐藏和其他动画属性的持续时间参数,那么我们将使用回调。因此,我的单功能侦听器将成为
$(document).click(function(e){
if( e.target.classList.contains('artists-home') ) {
$(e.target).hide(500,function(){
$('.ken-gallery').show();
});
}else if( e.target.classList.contains('ken-gallery') ){
$(e.target).hide(500,function(){
$('.artists-home').show();
});
}
});
编辑
请重新阅读您的问题,这些链接必须是页面跳转到顶部的链接,因此我将其添加到函数中。试试这个
$('.artist-ken').click(function(e){
e.preventDefault();
$('.artists-home').hide();
$('.ken-gallery').show();
});
您所做的不是show()
函数的实际参数。另外,即使是这样,您也没有指定它是一个类。它只能使用函数、持续时间、nothing或对象引用您还可以创建一个单向侦听器来显示另一个或隐藏另一个
$(document).click(function(e){
if( e.target.classList.contains('artists-home') ) {
e.preventDefault();
$(e.target).hide();
$('.ken-gallery').show();
}else if( e.target.classList.contains('ken-gallery') ){
e.preventDefault();
$(e.target).hide();
$('.artists-home').show();
}
});
Chonger还说,如果您想要一个淡入淡出,这是jQuery的任何显示/隐藏和其他动画属性的持续时间参数,那么我们将使用回调。因此,我的单功能侦听器将成为
$(document).click(function(e){
if( e.target.classList.contains('artists-home') ) {
$(e.target).hide(500,function(){
$('.ken-gallery').show();
});
}else if( e.target.classList.contains('ken-gallery') ){
$(e.target).hide(500,function(){
$('.artists-home').show();
});
}
});
编辑
重新阅读您的问题,这些必须是页面跳转到顶部的链接,因此我添加到了函数中。Dudeshow()
不能在()括号中包含参数,除非速度或动画方式(如show('slow')或show('1000')仅有效
你的语法错了
以下是有效的语法。
这意味着,用class“.artists home”隐藏div,用class“.ken gallery”显示div
欲了解更多信息,Dudeshow()
不能在()括号中包含参数,除非show('slow')或show('1000')等动画的速度或方式仅有效
你的语法错了
以下是有效的语法。
这意味着,用class“.artists home”隐藏div,用class“.ken gallery”显示div
更多信息使用回调函数有什么意义?无论如何都不会有任何动画。确保div仅在另一个被隐藏时显示的简单方法。由于hide
立即应用,浏览器将在同一帧中重新绘制计算出的差异。没有理由浪费更多的计算能力来创建另一个函数(尽管它非常便宜)。e
在这段代码中很可能是未定义的
。我想您的意思是在回调函数参数中包含e
参数?另外,解释一下为什么这样做可以提高这个答案的质量。如果你担心计算能力或者其他类似的问题,那么为什么你首先要使用jQuery?使用回调函数有什么意义?无论如何都不会有任何动画。确保div仅在另一个被隐藏时显示的简单方法。由于hide
立即应用,浏览器将在同一帧中重新绘制计算出的差异。没有理由浪费更多的计算能力来创建另一个函数(尽管它非常便宜)。e
在这段代码中很可能是未定义的
。我想您的意思是在回调函数参数中包含e
参数?另外,解释一下为什么这样做会提高这个答案的质量。如果你担心计算能力或诸如此类的问题,那么首先为什么要使用jQuery呢。我决定,我想让DIV淡入淡出。所以我用替换了.hide
,用.fadeOut
替换了,用.fadeIn
替换了.show
。它似乎起作用了。还有什么我需要补充的吗?或者有没有更优雅的方式来写呢?谢谢。我决定,我想让DIV淡入淡出。所以我用替换了.hide
,用.fadeOut
替换了,用.fadeIn
替换了.show
。它似乎起作用了。还有什么我需要补充的吗,或者有没有更优雅的方式来写呢?