我正在使用jQuery并希望检查元素的属性值(下面是Lodagema_Desc是否可见)。我明白我应该在这里应用这个布尔变量,但我不完全确定如何。我想检查这是真还是假(参见“if vis = true”)。
实际上,我正在尝试设置一个div,它会擦除整个页面并在点击prio_img
后变为可见。之后,我希望再次点击prio_img
基本上相反的动画。
$(document).ready(function() {
$("#prio_img").click(function() {
var vis = $("#Lodagema_Desc").is(":visible");
if (!vis){
$('#Lodagema_Desc').css({visibility: "visible"}).animate({
width: "1110px", //changes to visible, then animates to the correct width
}, 800 );}
else{
$('#Lodagema_Desc').animate({
width: "0px",
}, 800).css({visiblity: "hidden"});
}
});
});
编辑:我已经修复了语法错误,但代码仍然存在问题。
答案 0 :(得分:0)
您的语法错误
更改
if vis=false{
要
if (vis==false){
答案 1 :(得分:0)
使用==不=
if (vis==false){
或只是
if (!vis)
(表示错误)
答案 2 :(得分:0)
if
语句的条件需要放在括号()
中,如下所示:
if(vis) // == true
if(!vis) // == false
我已更新您的代码以修复您的陈述。注意,在语句反转之前添加!
。我也删除了var,因为你只使用过一次。您可以将条件直接放入if
以获得可读性。
$(document).ready(function(){
$("#prio_img").click(function() {
// The condition reads: if Lodagema_Desc is not visible, then...
if(!$("#Lodagema_Desc").is(":visible")){
$('#Lodagema_Desc').css({visibility: "visible"}).animate({
width: "1110px", //changes to visible, then animates to the correct width
}, 800 );}
else{
$('#Lodagema_Desc').animate({
width: "0px",
}, 800).css({visiblity: "hidden"});
}
});
});
修改强>
您也可以像这样使用:hidden
(以避免反转原始条件):
// This condition reads: if Lodagema_Desc is hidden, then...
if($("#Lodagema_Desc").is(":hidden")) {
// animate in
}
答案 3 :(得分:0)
您的if
语法错误。此外,您需要在函数内设置vis
变量。否则,它只包含页面加载时的可见性,而不是当前的可见性。
$(document).ready(function(){
$("#prio_img").click(function() {
var vis = $("#Lodagema_Desc").is(":visible");
if (!vis) {
$('#Lodagema_Desc').css({visibility: "visible"}).animate({
width: "1110px", //changes to visible, then animates to the correct width
}, 800 );
}
else{
$('#Lodagema_Desc').animate({
width: "0px",
}, 800).css({visiblity: "hidden"});
}
});
});
答案 4 :(得分:0)
http://jsfiddle.net/aamir/yZk5S/5/
试试这个
var $img = $('img'),
originalWidth = $img.width();
//Set height
$img.height($img.height());
$('a').click(function(){
$img.animate({ width: $img.width() == originalWidth ? 0 : originalWidth });
});
答案 5 :(得分:0)
你的条件if(vis === false)语法错误并抛出js错误。 以下代码工作正常
$(document).ready(function(){
var vis = $("#Lodagema_Desc").is(":visible");
$("#prio_img").click(function() {
if (vis===false){
$('#Lodagema_Desc').css({visibility: "visible"}).animate({
width: "1110px", //changes to visible, then animates to the correct width
}, 800 );}
else{
$('#Lodagema_Desc').animate({
width: "0px",
}, 800).css({visiblity: "hidden"});
}
});
});