myObject { div: htmlElement, foo1: bar1, foo2: bar2 }
密钥div
是原始密钥,它包含此对象的HTML
。从HTML访问对象?
是否有可能访问/获取/设置myObject
和其他价值观:foo1
和foo2
当我有范围htmlElement
,e.g点击它?
myObject
是原型,我不介意制作原型getter/setter,但我不知道如何通过/通过HTML
元素访问对象。
使对象:不介意谷歌地图的一部分,它只是一个普通对象..
function customHTML(latlng, foo, map) {
this.latlng = latlng;
this.foo = foo;
this.setMap(map);
}
customHTML.prototype = new google.maps.OverlayView();
customHTML.prototype.draw = function() {
var self = this;
var div = this.div;
//Create div if there's none
if (!div) {
div = this.div = document.createElement('div');
div.className = 'custom-html';
var panes = this.getPanes();
panes.overlayImage.appendChild(div);
}
//Location on map - not relevant for this question
var point = this.getProjection().fromLatLngToDivPixel(this.latlng);
if (point) {
div.style.left = (point.x - 20) + 'px';
div.style.top = (point.y - 64) + 'px';
}
};
//Create new objects
var data = /*parsed JSON*/;
var count = data.lenght;
var myMap = /*Google Map object - not relevant for this question*/;
for(i=0; i<count; i++) {
new CustomHTML(data[i].latlng, data[i].foo, myMap);
}
别的地方代码:
//Clicking element that was made above
jQuery('body').on('click', '.custom-html', function(event) {
//I need to access customHTML.foo for example, how?
//PS! I need to access it from outside, I cannot
//attach this click event in prototype.draw
}
'myObject'是JSON吗? – pratikpawar
@pratikwebdev数据是从JSON中获取的,但我构建了自己的原型'myObject',这只是浏览器内存中的一个对象,我需要通过'HTML' *(点击事件是精确的)*在视口中呈现并且是该对象的一部分。 – Solo
@Juhana为什么不呢?字符串格式的HTML元素?进一步渲染这个字符串可以用来渲染元素? – pratikpawar