$(".Personalized").click(function(){
$.ajax({
type:"POST",
url:"personalized.php",
cache:false,
beforeSend: function(){
$('#loading_personalized').show();
$('#triangle-personalized').show();
},
complete: function(){
$('#loading_personalized').hide();
},
success: function(html){
$("#divPersonalized").html(html).show();
}
});
});
当我点击个性化课程时,divPersoanlized显示出来,现在我想再次点击个性化隐藏它。我可以这样做吗...
答案 0 :(得分:5)
通常你只需使用toggle()
切换元素,但在这种情况下你可能不希望每次都运行ajax调用
$(".Personalized").click(function(){
if ( $("#divPersonalized").is(':visible') ) {
$("#divPersonalized").hide();
} else {
$.ajax({
type:"POST",
url:"personalized.php",
cache:false,
beforeSend: function(){
$('#loading_personalized').show();
$('#triangle-personalized').show();
},
complete: function(){
$('#loading_personalized').hide();
},
success: function(html){
$("#divPersonalized").html(html).show();
}
});
}
});
答案 1 :(得分:2)
例如:
$(".Personalized").click(function () {
var $divPersonalized = $("#divPersonalized");
if ($divPersonalized.is(':hidden')) {
$.ajax({
// ...
success: function (html) {
// Show the div
$("#divPersonalized").html(html).show();
}
});
}
else {
// Hide the div
$divPersonalized.hide();
}
});
答案 2 :(得分:0)
您可以检查div是否可见只是隐藏它。你不需要调用ajax
$(".Personalized").click(function(){
//You can use also use $(this).is(':visible')
if($(this).css('display') !== "none"){
$(this).hide();
return;
}
//Your ajax code
});
答案 3 :(得分:0)
在jquery中使用可见选择器。 visible doc.
你可以试试这个:
$(".Personalized").click(function(){
if($('#divPersonalized').is(':visible')) {
$('#divPersonalized').hide();
} else {
$.ajax({
...
...
success: function(html){
$("#divPersonalized").html(html).show();
}
...
...
});
}
});
答案 4 :(得分:-1)
您可以使用切换方法:
$(".Personalized").toggle(ShowFunction, HideFunction);
function ShowFunction(){
// your code goes here
$.ajax({
type:"POST",
url:"personalized.php",
cache:false,
beforeSend: function(){
$('#loading_personalized').show();
$('#triangle-personalized').show();
},
complete: function(){
$('#loading_personalized').hide();
},
success: function(html){
$("#divPersonalized").html(html).show();
}
});
}
function HideFunction(){
// your code goes here.
$("#divPersonalized").hide();
}
答案 5 :(得分:-1)
使用此:
if($("#divPersonalized").attr("hidden")=="hidden")
{
$("#divPersonalized").removeAttr("hidden");
}
else
{
$("#divPersonalized").attr("hidden","hidden");
}
<强>更新强>
我更新了演示。
答案 6 :(得分:-1)
投入混合,分离关注点(主观)的例子 - 值得一看。
(function() { /* wrap it to protect globals - optional */
/* 1) set some variables accessible to all functions, eg */
var contentdiv = $("#divPersonalized"), /* cache reused static selector(s) */
hasdataloaded = false;
/* 2) create a function to handle the data requests */
function loaddata() {
$.ajax({
type:"POST", url:"personalized.php", cache:false,
beforeSend: function(){
$('#loading_personalized').show();
$('#triangle-personalized').show();
},
complete: function(){
$('#loading_personalized').hide();
},
success: function(html){
hasdataloaded = true; /* updated the status */
contentdiv.html(html);
}
});
}
/* 3) A function to handle the show and hide and checki if data has loaded */
function toggleDataDiv() {
contentdiv.toggle(); /* showhide */
if(!hasdataloaded) { loadData(); } /* only want to load once */
}
/* 4) the event handler */
$(".Personalized").click(function(){ toggleDataDiv(); });
/* attach more events to other elements eg */
$(".someotherelement").click(function() { toggleDataDiv(); });
})();