我有兩個執行相同功能的JS文件,唯一的區別是被調用的.click觸發器和AJAX URL。 我想將腳本合併成一個。我相信這是可能的,但作爲一個JS新手有點發現它有點棘手。將2個Javascript文件合併爲一個
<script>
$(document).ready(function(){
// LOAD GAME JSON DATA VIA AJAX
$('.gameCta').click(function(){
id = $(this).children('span.title').attr('data-id');
// LOAD GAME PROVIDERS
$("#game_provs").load("http://localhost:8888/projects/superfreerespo/" + id + "/ .gameBox-Ops");
$.ajax({
url: "http://localhost:8888/projects/superfreerespo/" + id + "?json=get_category_posts&slug=games",
method: "GET",
data: {json: 1},
dataType: "JSON"}).done(function(data) {
// LOAD GAME INFORMATION
$("#game-name").html(data.post.title);
$("#game-reels").html(data.post.custom_fields.reels);
$("#game-paylines").html(data.post.custom_fields.paylines);
$("#game-minBet").html(data.post.custom_fields.min_bet);
$("#game-maxBet").html(data.post.custom_fields.max_bet);
$("#game-jackpot").html(data.post.custom_fields.jackpot);
$("#game-info").html(data.post.custom_fields.game_info);
$('#next').attr('data-id', data.next_url);
$('#previous').attr('data-id', data.previous_url);
// LOAD GAME THUMBNALS
var gameThumbSrc = new String(data.post.custom_fields.game_name);
gameThumbSrc = gameThumbSrc.replace(/ /g,'');
$('#gameBoxGallery').html('');
for(i = 0; i<= 2; i++){
image = '<img src="<?php bloginfo('template_directory'); ?>/images/games/screenshots/' + gameThumbSrc + '-' + i + '.jpg" class="gameThumb">'
$('#gameBoxGallery').append(image);
};
// ZOOM FIRST THUMBNAIL
$('#gameBox-Screenshot').html('');
image = '<img src="<?php bloginfo('template_directory'); ?>/images/games/screenshots/' + gameThumbSrc + '-0' + '.jpg" id="gameScreenshot">'
$('#gameBox-Screenshot').append(image);
})
})
});
</script>
<script>
$(document).on('click', '.direction', function(){
move = $(this).attr('data-id');
$.ajax({
url: move,
method: "GET",
data: {json: 1},
dataType: "JSON"}).done(function(data) {
// LOAD GAME INFORMATION
$("#game-header").html(data.post.title);
$("#game-name").html(data.post.title);
$("#game-reels").html(data.post.custom_fields.reels);
$("#game-paylines").html(data.post.custom_fields.paylines);
$("#game-minBet").html(data.post.custom_fields.min_bet);
$("#game-maxBet").html(data.post.custom_fields.max_bet);
$("#game-jackpot").html(data.post.custom_fields.jackpot);
$("#game-info").html(data.post.custom_fields.game_info);
$('#next').attr('data-id', data.next_url);
$('#previous').attr('data-id', data.previous_url);
// LOAD GAME THUMBNALS
var gameThumbSrc = data.post.custom_fields.game_name;
$('#gameBoxGallery').html('');
for(i = 0; i<= 2; i++){
image = '<img src="<?php bloginfo('template_directory'); ?>/images/games/' + gameThumbSrc + '/screenshots/' + gameThumbSrc + '-thumb-' + i + '.png" class="gameThumb">'
$('#gameBoxGallery').append(image);
};
var gameThumbSrc = data.post.custom_fields.game_name;
$('#gameBox-Screenshot').html('');
image = '<img src="<?php bloginfo('template_directory'); ?>/images/games/' + gameThumbSrc + '/screenshots/' + gameThumbSrc + '-large-0' + '.png" id="gameScreenshot">'
$('#gameBox-Screenshot').append(image);
})
});
// ZOOM THUMBNAIL ONCLICK
$(document).on('click', '.gameThumb', function(){
$('#gameScreenshot').attr('src',$(this).attr('src').replace('thumb','large'));
});
</script>
這似乎是我想要走的路線,但我有點失去了複製它,你可以擴展你的答案嗎? – James