DIV への html の Ajax ロードに関して質問がありました。理想的には、私が欲しいのはこれです:
ここにコードがある閉じるボタン付きのトグル div: http://jsfiddle.net/tymeJV/uhEgG/28/
$(document).ready(function () {
$('#country').click(function () {
$("#country_slide").slideToggle(function() {
if ($(this).is(":visible")) {
alert("im visible!");
}
});
});
$('#close').click(function (e) {
e.preventDefault();
$('#country_slide').slideToggle();
});
});
次に、divが展開されたときに、AJAXコードでhtmlファイルをdivにロードする必要があります。秘訣は、HTML が正常にロードされた場合、div が閉じられて再ロードされた場合に HTML ファイルを再度ロードするのを避けたいということです。私がこれのために持っているコード(ここから助けを得たのはこれです):
http://jsfiddle.net/spadez/uhEgG/55/
$(function () {
$('#country_link').on('click', function (e) {
// Prevent from following the link, if there is some sort of error in
// the code before 'return false' it would still follow the link.
e.preventDefault();
// Get $link because 'this' is something else in the ajax request.
var $link = $(this);
// Exit if the data is loaded already
if ($link.data('loaded') === true)
return false;
$.ajax({
type: 'GET',
dataType: 'html',
url: '/ajax/test.html',
timeout: 5000,
beforeSend: function () {
},
success: function (data, textStatus) {
$("#country_slide").html(data);
alert('request successful');
// If successful, bind 'loaded' in the data
$link.data('loaded', true)
},
error: function (xhr, textStatus, errorThrown) {
$("#country_slide").html('Error');
},
complete: function () {
},
});
});
});
私はまだこれを機能させることができませんでした。だから私の質問は、実際にこれを行うことは可能ですか?もしそうなら、jqueryの経験が豊富な人なら誰でもdivトグルをajaxロードスクリプトと統合するのを手伝ってくれますか?
これは私の最初の jquery スクリプトの 1 つであり、おそらく初心者向けではないので、少し苦労しています。ありがとうございました。