javascript - 如何在 “onclick” 事件期间加载外部 php 文件?

标签 javascript php jquery ajax

我试图在标签点击时加载谷歌地图以缩短页面加载时间。我在控制台中没有收到任何错误。只是空的 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> .似乎脚本没有在点击时执行。

我做错了什么?我需要说的是我还在学习。

map 脚本是这样工作的: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>

jquery.google-maps.js

// entire script above
// autoload maps
jQuery(function($) { $('.gmap').googleMaps(); });

先试试

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

第二次尝试

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

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);
                }
            });
        });
    });
});

最佳答案

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

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();
    });
  });

关于javascript - 如何在 “onclick” 事件期间加载外部 php 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34491969/

相关文章:

javascript - jQuery 动态添加行数到表中?

javascript - 如何将项目从一个列表移动到 HTML 中的另一个列表

javascript - 使用其他语言时引导日期选择器错误 : Uncaught TypeError: Cannot read property 'length' of undefined

php - 多选菜单

javascript - jQuery 切换延迟以及类和淡入淡出

jquery - Onload Jquery循环通过div :hovers?

javascript - AngularJS:从 DOM 中删除 HTML 元素,但不删除其内容

javascript - 无法从ajax获取post数据到php

php - 如何使用 php 将 GPS 坐标转换为完整地址?

php - 无法使用 PHP 从 MySQL 获取数据