You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除谷歌地图自定义标记信息窗口的Close标题属性

How to Remove the "Close" Title from Google Maps InfoWindow's Close Button

The issue you're facing is that when you call infowindow.open(), Google Maps renders the info window's DOM elements asynchronously. Your jQuery code runs right after open() is called, but the .gm-ui-hover-effect element hasn't been added to the DOM yet—that's why it works when you run it manually later.

Here are a few reliable solutions to fix this:

Google Maps provides a domready event specifically for when the info window's DOM has fully loaded. This is the most stable approach because it's triggered exactly when the elements you need are available.

Modify your bindInfoWindow function like this:

function bindInfoWindow(marker, map, infowindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(html);
    infowindow.open(map, marker);
    
    // Wait for the info window's DOM to be fully rendered
    google.maps.event.addListenerOnce(infowindow, 'domready', function() {
      jQuery(".gm-ui-hover-effect").attr('title', '');
    });
  });
}

The addListenerOnce ensures this code only runs once per info window open, preventing duplicate event bindings.

2. Fallback: Use MutationObserver to Detect DOM Changes

If for some reason the domready event doesn't work (rare, but possible with custom setups), you can use a MutationObserver to watch for when the close button element is added to the DOM:

function bindInfoWindow(marker, map, infowindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(html);
    infowindow.open(map, marker);
    
    const observer = new MutationObserver(function(mutations) {
      const closeButton = document.querySelector(".gm-ui-hover-effect");
      if (closeButton) {
        closeButton.setAttribute('title', '');
        observer.disconnect(); // Stop observing once we've made the change
      }
    });
    
    // Watch the entire document for new elements (you can narrow this to a specific container if needed)
    observer.observe(document.body, {
      childList: true,
      subtree: true
    });
  });
}

Why Your Original Code Failed

When you call infowindow.open(), Google Maps starts rendering the info window in the background. Your jQuery selector runs before the .gm-ui-hover-effect element exists in the DOM, so it can't find anything to modify. Waiting for the domready event or using a mutation observer ensures your code runs only when the element is available.

内容的提问来源于stack exchange,提问作者Bing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:40:31