gpt4 book ai didi

javascript - 将 JS 文件包含在另一个 JS 文件中

转载 作者:行者123 更新时间:2023-12-03 05:07:48 26 4
gpt4 key购买 nike

我需要一些帮助。所以,我有一个 js 文件,其中包含 map 主题。我想将它包含到我的另一个js文件中,并调用所有主题以创建选择 map 主题的选项。

这是我的 map 主题代码:

     green_theme: [{
"featureType": "water",
"elementType": "geometry",
"stylers": [{
"visibility": "on"
}, {
"color": "#aee2e0"
}]
}, {
"featureType": "landscape",
"elementType": "geometry.fill",
"stylers": [{
"color": "#abce83"
}]
}, {
"featureType": "poi",
"elementType": "geometry.fill",
"stylers": [{
"color": "#769E72"
}]
}, {
"featureType": "poi",
"elementType": "labels.text.fill",
"stylers": [{
"color": "#7B8758"
}]
}, {
"featureType": "poi",
"elementType": "labels.text.stroke",
"stylers": [{
"color": "#EBF4A4"
}]
}, {
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [{
"visibility": "simplified"
}, {
"color": "#8dab68"
}]
}, {
"featureType": "road",
"elementType": "geometry.fill",
"stylers": [{
"visibility": "simplified"
}]
}, {
"featureType": "road",
"elementType": "labels.text.fill",
"stylers": [{
"color": "#5B5B3F"
}]
}, {
"featureType": "road",
"elementType": "labels.text.stroke",
"stylers": [{
"color": "#ABCE83"
}]
}, {
"featureType": "road",
"elementType": "labels.icon",
"stylers": [{
"visibility": "off"
}]
}, {
"featureType": "road.local",
"elementType": "geometry",
"stylers": [{
"color": "#A4C67D"
}]
}, {
"featureType": "road.arterial",
"elementType": "geometry",
"stylers": [{
"color": "#9BBF72"
}]
}, {
"featureType": "road.highway",
"elementType": "geometry",
"stylers": [{
"color": "#EBF4A4"
}]
}, {
"featureType": "transit",
"stylers": [{
"visibility": "off"
}]
}, {
"featureType": "administrative",
"elementType": "geometry.stroke",
"stylers": [{
"visibility": "on"
}, {
"color": "#87ae79"
}]
}, {
"featureType": "administrative",
"elementType": "geometry.fill",
"stylers": [{
"color": "#7f2200"
}, {
"visibility": "off"
}]
}, {
"featureType": "administrative",
"elementType": "labels.text.stroke",
"stylers": [{
"color": "#ffffff"
}, {
"visibility": "on"
}, {
"weight": 4.1
}]
}, {
"featureType": "administrative",
"elementType": "labels.text.fill",
"stylers": [{
"color": "#495421"
}]
}, {
"featureType": "administrative.neighborhood",
"elementType": "labels",
"stylers": [{
"visibility": "off"
}]
}],

black_theme: [{
"stylers": [{
"hue": "#ff1a00"
}, {
"invert_lightness": true
}, {
"saturation": -100
}, {
"lightness": 33
}, {
"gamma": 0.5
}]
}, {
"featureType": "water",
"elementType": "geometry",
"stylers": [{
"color": "#2D333C"
}]
}],

blue_theme: [{
"featureType": "landscape.natural",
"elementType": "geometry.fill",
"stylers": [{
"visibility": "on"
}, {
"color": "#e0efef"
}]
}, {
"featureType": "poi",
"elementType": "geometry.fill",
"stylers": [{
"visibility": "on"
}, {
"hue": "#1900ff"
}, {
"color": "#c0e8e8"
}]
}, {
"featureType": "road",
"elementType": "geometry",
"stylers": [{
"lightness": 100
}, {
"visibility": "simplified"
}]
}, {
"featureType": "road",
"elementType": "labels",
"stylers": [{
"visibility": "off"
}]
}, {
"featureType": "transit.line",
"elementType": "geometry",
"stylers": [{
"visibility": "on"
}, {
"lightness": 700
}]
}, {
"featureType": "water",
"elementType": "all",
"stylers": [{
"color": "#7dcdcd"
}]
}],

retro_theme: [{
"featureType": "administrative",
"stylers": [{
"visibility": "off"
}]
}, {
"featureType": "poi",
"stylers": [{
"visibility": "simplified"
}]
}, {
"featureType": "road",
"elementType": "labels",
"stylers": [{
"visibility": "simplified"
}]
}, {
"featureType": "water",
"stylers": [{
"visibility": "simplified"
}]
}, {
"featureType": "transit",
"stylers": [{
"visibility": "simplified"
}]
}, {
"featureType": "landscape",
"stylers": [{
"visibility": "simplified"
}]
}, {
"featureType": "road.highway",
"stylers": [{
"visibility": "off"
}]
}, {
"featureType": "road.local",
"stylers": [{
"visibility": "on"
}]
}, {
"featureType": "road.highway",
"elementType": "geometry",
"stylers": [{
"visibility": "on"
}]
}, {
"featureType": "water",
"stylers": [{
"color": "#84afa3"
}, {
"lightness": 52
}]
}, {
"stylers": [{
"saturation": -17
}, {
"gamma": 0.36
}]
}, {
"featureType": "transit.line",
"elementType": "geometry",
"stylers": [{
"color": "#3f518c"
}]
}]

这是我的另一个 js 文件的代码:

var options = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
//mapTypeId: google.maps.MapTypeId.HYBRID ,
mapTypeControl: true,
streetViewControl: true,
center: new google.maps.LatLng(14.435947810360242, 121.01270109415054),
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL,
position: google.maps.ControlPosition.RIGHT_BOTTOM
}
};

//refresh map
var map = new google.maps.Map(document.getElementById('map'), options);

提前致谢!

最佳答案

您不能包含 JS 文件,您可以按照您想要的顺序将它们包含到 HTML 文件中,如下所示:

<head>
<script type="text/javascript" src="/first_js.js" ></script>
<script type="text/javascript" src="/second_js.js" ></script>
</head>
<body>
</body>

您还可以使用 jQuery 的获取脚本功能:

$.getScript("second_js.js", function(){

alert("Script loaded.");

});

或者您可以通过编程方式将任何 JS 包含到 HTML 中,如下所示:

function loadScript(url, callback)
{
// Adding the script tag to the head as suggested before
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;

// Then bind the event to the callback function.
// There are several events for cross browser compatibility.
script.onreadystatechange = callback;
script.onload = callback;

// Fire the loading
head.appendChild(script);
}

关于javascript - 将 JS 文件包含在另一个 JS 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41951608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com