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