如何调整信息窗口的大小并点击按钮?+更改信息窗口的内容
How can I resize infowindow with button onclick in it? + Change content of infowindow
我尝试了很多不同的方法来写这篇文章,但我真的不明白该怎么做。我是javascript的新手,所以也许这对你来说很容易。
当我点击地图上的标记时,我有一个信息框。我想在那个信息框里有"更多"按钮。当有人点击按钮时,信息框将调整大小并更改内容。如何使用javascript和HTML编写它?
resizeWindow = function (stringItem, isResized) {
var eventItem = JSON.parse(stringItem);
processLocation(eventItem, isResized);
};
processLocation = function (eventItem, isResized) {
var i, contentString, myOptions, ib, markerOptions, marker, infoWindowWidth;
console.log("---> event is stoned: " + eventItem.name);
var stringItem = JSON.stringify(eventItem);
if (!isResized) {
infoWindowWidth = "450px";
contentString =
'<div class="eventitem-content">' +
'<div class="eventitem-bodycontent">' +
'<div class="eventitem-title">' + eventItem.name + '</div><img src="/Content/LandfillPhotos/' + eventItem.photo_number + '.png" alt="Landfill" style="width:425px;height:335px"/>' +
'<div class="eventitem-company">' + eventItem.company + '</div><div class="eventitem-address">' + eventItem.company_address + '</div>' +
'<button onclick="FBN.events.resizeWindow('+ stringItem + ', true)">Více</button>' +
'</div>' +
'</div>';
console.log(isResized);
} else {
infoWindowWidth = window.screen.availWidth + "px";
contentString =
'<div class="eventitem-content">' +
'<div class="eventitem-bodycontent">' +
'<div class="eventitem-title">' + eventItem.name + '</div><img src="/Content/LandfillPhotos/' + eventItem.photo_number + '.png" alt="Landfill" style="width:425px;height:335px"/>' +
'<div class="eventitem-company">' + eventItem.company + '</div><div class="eventitem-address">' + eventItem.company_address + '</div>' +
'<button onclick="FBN.events.resizeWindow(' + stringItem + ', false)">Více</button>' +
'</div>' +
'</div>';
}
myOptions = {
content: contentString,
disableAutoPan: false,
maxWidth: 0,
pixelOffset: new google.maps.Size(-140, 4),
zIndex: null,
boxStyle: {
width: infoWindowWidth
},
closeBoxMargin: "6px",
closeBoxURL: "https://www.google.com/intl/en_us/mapfiles/close.gif",
infoBoxClearance: new google.maps.Size(1, 1),
isHidden: false,
pane: "floatPane",
enableEventPropagation: false
};
ib = new InfoBox(myOptions);
infoWindows.push(ib);
markerOptions = {
position: new google.maps.LatLng(eventItem.latitude, eventItem.longitude),
map: map,
title: eventItem.name,
icon: 'https://cdn1.iconfinder.com/data/icons/Map-Markers-Icons-Demo-PNG/32/Map-Marker-Marker-Outside-Azure.png'
}
marker = new google.maps.Marker(markerOptions);
markers.push(marker);
createListener(marker, map, ib);
};
当我这样做时,控制台会给我写:Unexpected token;
'<button onclick="FBN.events.resizeWindow('+ stringItem + ', true)">Více</button>' +
这行是的问题
这是一个真正涉及的情况,一旦解决了这个直接的问题,变量作用域很快就会出现更多问题。我将向您展示如何解决有关Unexpected token ;
的即时问题。
您在引号和JSON方面遇到了困难。以您的代码为例:
'<button onclick="FBN.events.resizeWindow('+ stringItem + ', true)">Více</button>'
您的stringItem
不再只是一个变量,而是其内容将在上面内联。如果它有任何未带转义的单引号或双引号,则会导致错误。
以下是我回收的stringItem
样品:
{"name":"Skládka odpadů S-OO3 se sektorem S-OO1 Ďáblice","county_code":"CZ010", ... ,"longitude":"14.482411","photo_number":"1"}
换句话说,您实际上是在编写这个动态HTML。注意双引号:
<button onclick="FBN.events.resizeWindow({"name":"Skládka odpadů S-OO3 se sektorem S-OO1 Ďáblice","county_code":"CZ010", ... ,"longitude":"14.482411","photo_number":"1"}, true)">Více</button>
首先,您需要用单引号将这个文本字符串括起来。所以我们需要这个:
'<button onclick="FBN.events.resizeWindow('''+ stringItem + ''', true)">Více</button>'
这是足够的信息,你可以解决你的问题,但更进一步,而不是
var stringItem = JSON.stringify(eventItem);
您应该编码stringItem
更多以删除所有引号。我得到了你的代码工作与此:
var stringItem = utf8_to_b64(JSON.stringify(eventItem));
其中我包含了这些有用的函数,它们的名称不言自明:
function utf8_to_b64( str ) {
return window.btoa(unescape(encodeURIComponent( str )));
}
function b64_to_utf8( str ) {
return decodeURIComponent(escape(window.atob( str )));
}
当我对你提供的网站进行修改时(使用Chrome开发工具),你的"Vice"按钮现在呈现为这样的
<button onclick="FBN.events.resizeWindow('eyJuYW1lIj ... IjoiMyJ9', true)">Více</button>
这现在是有效的语法。您的新问题是onclick处理程序希望在哪里找到FBN.events.resizeWindow()
。但这是另一个问题。:)
以下是我尝试过的一些变化:http://pastebin.com/Uczwct8H
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 防止Iframe窗体在新窗口中打开
- 内部分区字体大小获胜'调整浏览器窗口大小时不会随媒体查询而更改
- 调整窗口大小时,可拖动的对象会出现在容器外部
- Javascript排序的图像弹出窗口..可以't单独弹出
- 窗口大小html css
- 弹出窗口出现,然后退出
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- Javascript更新孙窗口中的表单元素
- 如何在选项卡上定义属性'的主窗口对象
- CSS-若窗口太小,滚动条会出现在“表格”单元格上
- 召回窗口加载事件 - javascript
- 为什么不显示警报窗口
- 如何使用modalDialog来显示可以在所有浏览器中工作的弹出窗口
- Chrome应用程序调整窗口大小保持纵横比
- 新选项卡被弹出窗口阻止程序阻止
- 如何访问UIWebView'的子窗口上下文
- 刷新父窗口后无法关闭窗口
- 自动关闭弹出窗口的Javascript
- 如何使用特定大小的浏览器窗口打开我的页面