读取 javascript 变量并添加到 html iframe src



在下面的代码中,我有 2 个 JavaScript 变量长和纬度

我的挑战是能够将这些值附加到 iframe URL 的末尾

协助我对以下代码进行更改以允许我实现上述目标,将不胜感激

下面的 iframe 代码我需要能够将数值"-37.718821,145.064612"替换为 js 变量经度,下面的代码从页面 url 动态确定

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta name="viewport" content="initial-scale=1, maximum-scale=1"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script>
            function gup(name) {
                name = name.replace(/[[]/, "\[").replace(/[]]/, "\]");
                var regexS = "[\?&]" + name + "=([^&#]*)";
                var regex = new RegExp(regexS);
                var results = regex.exec(window.location.href);
                if (results == null)
                    return "";
                else
                    return results[1];
            }
            var latlong = (gup('q'));
            var parts = latlong.split(",");
            var lat = parts[0];
            var long = parts[1];

        </script>

    </head>
    <body>
        <div data-role="page" id="map_display_Page">
            <div data-role="header" id="myheader" data-position="fixed" style="background: #ffffff"></div>
            <div data-role="header" id="myheader" data-position="fixed" style="color: #ffffff; background: #000033;">
                <a href="#" data-role="button" data-rel="back" data-icon="back">Back</a>
                <h1>Map Location</h1>
            </div>
            <div data-role="content">
                <iframe src="maps1.html?q=-37.718821,145.064612" style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden;"></iframe>  --->          </div>
            <div data-role="footer" data-theme="a" data-position="fixed" data-id="myfooter" style="color: #ffffff; background: #000033;">
                <div class="ui-bar">
                    <a href="sharedialog.html"  data-role="button" data-icon="star" data-theme="a" data-rel="dialog">Share</a>
                    <a href="mailto:info@TrackingCentral.com.au?subject=Iphone App Enquiry" data-role="button" data-icon="plus" data-theme="a">Contact</a>
                    <a href="" data-role="button" data-icon="arrow-u" data-theme="a" style="float:right;" class="returnTopAction">Return top</a>
                </div>
            </div>
        </div>
    </body>
</html>

试试这个:

$("iframe").attr("src", "maps1.html?q="+ lat +"," + long +");

这只是更改iframe的 src 属性,并将其替换为您的变量 latlong

试试这个应该可以工作

$(document).ready(function(){
$("iframe").attr("src","maps1.html?q="+lat+","+long);
});

$("iframe").src="maps1.html?q="+lat+","+long;

最新更新