如何移除谷歌地图自定义标记信息窗口的Close标题属性
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:
1. Use the InfoWindow's domready Event (Recommended)
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

