gpt4 book ai didi

javascript - 信息窗口按钮 jQuery 事件

转载 作者:行者123 更新时间:2023-11-30 07:55:27 24 4
gpt4 key购买 nike

当有人在 infoWindow div 上单击“编辑”、“共享”或“删除”时,我无法执行调用的 jQuery 函数。

var markers = [];
for(i=0; i<array.length; ++i) {
var marker = new google.maps.Marker({
position: {lat: parseFloat(array[i]['latitude']), lng: parseFloat(array[i]['longitude'])},
map: map
});

var id = array[i]['id'];
var edit = 'edit', share = 'share', del = 'delete';
var cString = '<div style="margin: auto; text-align: center; font-family: Tahoma, Geneva, sans-serif;"><strong>Location Name: </strong>' + array[i]['title']
+ '<br><strong>Location Description: </strong>' + array[i]['description']
+ '<br><br><br><div class="btn-group"><button type="button" class="btn btn-primary '+edit+'" id="' + id + '">Edit</button><button type="button" class="btn btn-primary '+share+'" id="' + id + '">Share</button><button type="button" class="btn btn-primary '+del+'" id="' + id + '">Delete</button></div>';

contentString.push(cString);
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infoWindow.setContent(contentString[i]);
infoWindow.open(map, marker);
}
})(marker, i));

// this is the function
$('button').click(function() {
console.log('clicked');
});

markers.push(marker);
}

它不会为分配给 infoWindow 的按钮显示已点击,但会显示其他按钮,如注销、查看个人资料等。

Array 是一个具有以下结构的 JSON 数组:

[
{
id:"1"
description:"I am Loving It! ↵McArabia Combo Meal: 520 Rs/-"
latitude:"25.28919345"
longitude:"67.11113134"
title:"McDonalds"
type:"favourite"
},//....
//......
]

我该如何解决这个问题?

最佳答案

进一步的@anu 评论:

那是因为 infoWindow 只有在函数 infoWindow.open(map, marker); 中才会添加到 DOM,所以当你绑定(bind) click 添加到按钮,infoWindow 的按钮不包括在内。

和实例:

$(document).on('click', '.info-button', function(){
alert('button clicked');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Info windows</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>

// This example displays a marker at the center of Australia.
// When the user clicks the marker, an info window opens.

function initMap() {
var uluru = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});

var contentString = '<div id="content">'+
'<button class="info-button">Click on it</button>' +
'</div>';

var infowindow = new google.maps.InfoWindow({
content: contentString
});

var marker = new google.maps.Marker({
position: uluru,
map: map,
title: 'Uluru (Ayers Rock)'
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?&callback=initMap">
</script>
</body>
</html>

关于javascript - 信息窗口按钮 jQuery 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41039505/

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