简体   繁体   English

使用javascript更改div id和innerHTML内容

[英]Change div id and innerHTML content with javascript

Hi i'm trying to change a google map and h3 text with javascript, but it's not working. 嗨,我正在尝试使用javascript更改Google地图和h3文本,但无法正常工作。

What i want to do is display different maps and headers, so basically i want to replace the <div id="map"></div> id with id="map2" to display the Berlin Location instead of Los Angeles. 我要显示的是不同的地图和标题,所以基本上我想用id="map2"替换<div id="map"></div> id以显示柏林位置而不是洛杉矶。 Also i want to replace the <h3 div="textChange">Worlds Finals Season 3 and 6</h3> text with "Worlds Season 5" . 我也想将 <h3 div="textChange">Worlds Finals Season 3 and 6</h3>文本替换为 "Worlds Season 5"

Here's the HTML: 这是HTML:

<article>
<div class="container">
<button onclick="changediv()">Season 5</button>
<h3 id="textChange">Worlds Finals Season 3 and 6</h3>
<div id="map"></div>
</div>
</article>

Here's the Javascript: 这是Javascript:

function initMap() {
    var losAngeles = {
        lat: 34.038037,
        lng: -118.244753
    };
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 4,
        center: losAngeles
    });
    var marker = new google.maps.Marker({
        position: losAngeles,
        map: map
    });

    var berlin = {
        lat: 52.518894,
        lng: 13.407413
    };
    var map2 = new google.maps.Map(document.getElementById('map2'), {
        zoom: 4,
        center: berlin
    });

function changediv() {
    if (document.getElementById("map")) {
        document.getElementById("textChange").innerHTML = "Worlds Finals Season 5";          
        document.getElementById("map").id = "map2";
    }
}

EDIT: "onlick" "div" and parantheses are added, so now the h3 text changes, but the map still won't change, do i have to relaod the page for that to work? 编辑:“ onlick”“ div”和括号被添加,所以现在h3文本更改,但地图仍然不会更改,我必须重新声明页面才能正常工作吗? Using google maps api. 使用Google Maps API。

1) there's a typo here: 1)这里有一个错字:

<h3 div="textChange">Worlds Finals Season 3 and 6</h3>

change it to: 更改为:

<h3 id="textChange">Worlds Finals Season 3 and 6</h3>

2) As mentioned in the comment there's another typo in the html 2)如评论中所述,HTML中还有另一种错字

<button onlick="changediv">Season 5</button>

when you trigger a click you want the function to be invoked, you forgot the parenthesis. 触发单击时,如果要调用该函数,则会忘记括号。

<button onlick="changediv()">Season 5</button>

3) last but not least, the map. 3)最后但并非最不重要的一点是地图。 . Map instances according to the doc should be reused, instead of creating a new instance you should just change the location. 应根据文档重用地图实例,而不是创建新实例,而只需更改位置。

function changeDiv(){
  if (document.getElementById("map")) {
    document.getElementById("textChange").innerHTML = "Worlds Finals Season5";          
    var location = new google.maps.LatLng(berlin.lat, berlin.lng);
    map.panTo(location)
  }

}

You can simply move the map with map.setCenter(LatLng). 您只需使用map.setCenter(LatLng)移动地图即可。 This method will not use any other markers and will not erase existing markers. 此方法将不使用任何其他标记,也不会删除现有标记。 You could also move the map via the panTo(LatLng) Both methods are also available after the map has been initialized. 您也可以通过panTo(LatLng)移动地图。初始化地图后,这两种方法也都可用。

You can read more here https://developers.google.com/maps/documentation/javascript/reference#Map 您可以在这里阅读更多内容https://developers.google.com/maps/documentation/javascript/reference#Map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM