如何在"onclick"事件期间加载外部php文件?

Fro*_*urn 5 javascript php ajax jquery

我试图在选项卡上加载谷歌地图点击以缩短页面加载.我在控制台中没有收到任何错误.只是空div <div class="gmap"></div>,这应该是这样的:<div class="gmap"><iframe style="border: none;" width="350px" height="150px" src="http://maps.google.com/maps?myadress"></iframe></div>.似乎脚本不是在点击时执行的.

我究竟做错了什么?我需要提一下我还在学习.

这是地图脚本的工作方式:JSFIDDLE

map.php

<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../../..' ));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe = JFactory::getApplication('site');

jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('mod_module');
$moduleParams = new JRegistry();
$moduleParams->loadString($module->params);
?>
<div class="gmap" data-address="" data-lang="auto" data-width="350px" data-height="150px" data-zoom="12" data-bubble="false" data-pin-size="1" data-output="iframe"></div>
Run Code Online (Sandbox Code Playgroud)

jquery.google-maps.js

// entire script above
// autoload maps
jQuery(function($) { $('.gmap').googleMaps(); });
Run Code Online (Sandbox Code Playgroud)

第一次尝试

jQuery(document).ready(function($) {
    $(".tab").click(function() {
        $.getScript(window.location.origin + "jquery.googlemap.js", function() {
            $(".div-inner").load("map.php");
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

第二次尝试

我也试过这种方式,但没有效果:

jQuery(document).ready(function($) {
    $(".tab").click(function() {
        $.getScript(window.location.origin + "/jquery.googlemap.js", function() {
            $.ajax({
                type: "GET",
                cache: false,
                url: '/map.php',
                success: function(data) {
                    $('.div-inner').html(data);
                }
            });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

Fro*_*urn 0

我找到了解决方案。谢谢!

jQuery(document).ready(function($) {
  $(".tab").one('click', function() {
    $(".div-inner").append('<div class="gmap" data-address="New York"></div>');
    $.getScript(window.location.origin + '/jquery.googlemap.js', function() {
      $('.gmap').googleMaps();
    });
  });
Run Code Online (Sandbox Code Playgroud)