Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MarkerWithLabel labelContent div onclick事件_Javascript_Css_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript MarkerWithLabel labelContent div onclick事件

Javascript MarkerWithLabel labelContent div onclick事件,javascript,css,google-maps,google-maps-api-3,Javascript,Css,Google Maps,Google Maps Api 3,我已经创建了一个包含几个DIV的标记,我希望能够访问单个DIV上的click事件,而不仅仅是整个标记 这是我的记号笔: var marker = new MarkerWithLabel({ position: myMap.getCenter(), map: myMap, opacity: 0.0, labelContent: "<div id=container>" + " <div id=largeDiv" + "></div>" +

我已经创建了一个包含几个DIV的标记,我希望能够访问单个DIV上的click事件,而不仅仅是整个标记

这是我的记号笔:

var marker = new MarkerWithLabel({
  position: myMap.getCenter(),
  map: myMap,
  opacity: 0.0,
  labelContent: "<div id=container>" +
  " <div id=largeDiv" +
  "></div>" +
  " <div id=smallDiv" +
  "></div>" +
  "</div>",
  labelAnchor: new google.maps.Point(width/2, height/2),
  labelClass: "labelClass" // the CSS class for the label
});
第三个解决方案显然是一个糟糕的解决方案,我更愿意在创建DIV后立即添加click侦听器。我怎样才能最好地完成这一点


在将div添加到标记之前,将其创建为jQuery HTML元素。同时向其添加事件侦听器。e、 g.我认为这应该有效:

var yourDiv = $("<div id=container>" +
  " <div id=largeDiv" +
  "></div>" +
  " <div id=smallDiv" +
  "></div>" +
  "</div>");

yourDiv.on("click", function() {
  alert("clicked largeDiv");
});

var marker = new MarkerWithLabel({
  position: myMap.getCenter(),
  map: myMap,
  opacity: 0.0,
  labelContent: yourDiv,
  labelAnchor: new google.maps.Point(width/2, height/2),
  labelClass: "labelClass" // the CSS class for the label
});
var yourDiv=$(“”+
" " +
" " +
"");
yourDiv.on(“单击”,函数(){
警报(“单击大除数”);
});
变量标记=新标记WithLabel({
位置:myMap.getCenter(),
地图:我的地图,
不透明度:0.0,
标签内容:yourDiv,
labelAnchor:新的google.maps.Point(宽度/2,高度/2),
labelClass:“labelClass”//标签的CSS类
});

不起作用。似乎我不能在labelContent中以这种方式引用它,它只在我设置labelContent:yourDiv.html()时起作用,但我假设它只生成它的一个副本。
var yourDiv = $("<div id=container>" +
  " <div id=largeDiv" +
  "></div>" +
  " <div id=smallDiv" +
  "></div>" +
  "</div>");

yourDiv.on("click", function() {
  alert("clicked largeDiv");
});

var marker = new MarkerWithLabel({
  position: myMap.getCenter(),
  map: myMap,
  opacity: 0.0,
  labelContent: yourDiv,
  labelAnchor: new google.maps.Point(width/2, height/2),
  labelClass: "labelClass" // the CSS class for the label
});
//it works

var div = document.createElement('div'),
    textElem = document.createTextNode('label');
div.appendChild(textElem);
$(div).on("click", function () {
  alert("click");
});

var marker = new MarkerWithLabel({
  position: myMap.getCenter(),
  map: myMap,
  opacity: 0.0,
  labelContent: div,
  labelAnchor: new google.maps.Point(width/2, height/2),
  labelClass: "labelClass" // the CSS class for the label
});