[英]How to make click toggle event
我正在嘗試使用此代碼進行點擊切換事件
的HTML
<a class="load" data-gallery="123456" style="cursor: pointer;"><h2><p>example</p></h2></a>
<div id="123456">
</div>
jQuery的
$('.link').toggle(function () {
var id = $(this).data('gallery');
var postData = {
"c": id
};
$.ajax({
type: 'GET',
data: postData,
url: 'images.php',
beforeSend: function () {
$("#" + id).html("Loading...");
},
success: function (data) {
$("#" + id).html(data);
},
error: function () {
alert('Error');
}
});
}, function () {
var id = $(this).data('gallery');
$("#" + id).html("");
});
但是使用此代碼,.load()不可點擊
謝謝
toggle事件已從jQuery 1.9+
刪除,您可以使用以下邏輯來代替click
事件:
$('.link').click(function () {
$(this).data('toggle', !$(this).data('toggle'));
if ($(this).data('toggle')) {
var id = $(this).data('gallery');
var postData = {
"c": id
};
$.ajax({
type: 'GET',
data: postData,
url: 'images.php',
beforeSend: function () {
$("#" + id).html("Loading...");
},
success: function (data) {
$("#" + id).html(data);
},
error: function () {
alert('Error');
}
});
} else {
var id = $(this).data('gallery');
$("#" + id).html("");
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.