[英]appending div in JS as string(?)
I'm using the Google Maps API, and I'm trying to have a "card" appear (as apposed to a tooltip/infobox) when clicking on a marker. 我正在使用Google Maps API,并且试图在单击标记时出现“卡”(显示在工具提示/信息框上)。 I followed this to get the card to appear. 我按照此操作使卡片出现。 However, the method this person uses is appending a huge string that is the html for a white box with all the links/information hard coded into this string. 但是,此人使用的方法是在白框的html后面附加一个巨大的字符串,并将所有链接/信息硬编码到该字符串中。
I'm trying to change this so it can dynamically insert the desired information (I've stripped down the code of the card so it simply displays a grey box when clicking on the marker, you can see the code here ). 我正在尝试更改此设置,以便它可以动态插入所需的信息(我已经删除了卡的代码,因此单击标记时它仅显示一个灰色框,您可以在此处看到代码)。
The important bit of code is:` 代码的重要部分是:
google.maps.event.addDomListener(marker, 'click', function() {
// Prevents card from being added more than once (i.e. when page is resized and google maps re-renders)
if ( $( ".place-card" ).length === 0 ) {
var rootDiv = document.createElement('div');
rootDiv.style = ('position', 'absolute');
rootDiv.style = ('right', '0px');
rootDiv.style = ('top', '0px');
var secondDiv = document.createElement('div');
secondDiv.style = ('margin', '10px');
secondDiv.style = ('padding', '1px');
secondDiv.style = ('-webkit-box-shadow', 'rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px');
secondDiv.style = ('box-shadow', 'rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px');
secondDiv.style = ('border-radius', '2px');
secondDiv.style = ('background-color', 'white');
secondDiv.style = ('width', '200px');
secondDiv.style = ('height', '150px');
rootDiv.appendChild(secondDiv);
// this does not work
// $(".gm-style").append(rootDiv);
// this works
$(".gm-style").append('<div style="position: absolute; right: 0px; top: 0px;"><div style="margin: 10px; padding: 1px; -webkit-box-shadow: rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px; box-shadow: rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px; border-radius: 2px; background-color: white; width: 200px; height: 150px"></div></div>');
}
});
(You can see I'm trying to replace the str2
with actual div's... I've also tried it without calling String()
which doesn't work either). (您可以看到我正在尝试将str2
替换为实际的div。。。我也尝试过不调用String()
也不起作用)。 I'm wondering if there is a better way than simply appending strings together to make this card appear. 我想知道是否有比简单地将字符串附加在一起以使此卡显示更好的方法。
Also, if someone could explain what $(".gm-style")
does that would be lovely. 另外,如果有人可以解释$(".gm-style")
作用,那将很可爱。 (My understanding after much googling is that it is an alternative for document.getElementById
, but .gm-style
is a class name... and any substitute "get___by___" function I try just results in error). (经过大量的搜索后,我的理解是它是document.getElementById
的替代方法,但是.gm-style
是类名...,我尝试使用的任何替代方法“ get___by___”都只会导致错误)。
Thanks! 谢谢!
It's possible to get the HTML code for the element as a string, but it's rather pointless. 可以将元素的HTML代码作为字符串获得,但这毫无意义。 Appending the string will just recreate the element. 追加字符串只会重新创建元素。
Just append the element that you have: 只需追加您拥有的元素:
$(".gm-style").append(rootDiv);
Your code for setting the styles are all wrong. 您用于设置样式的代码都是错误的。 An expression like ('position', 'absolute')
uses the comma expression, so its value is just the string 'absolute'
. 像('position', 'absolute')
这样的表达式使用逗号表达式,因此其值只是字符串'absolute'
。 Assigning that string to the style doesn't work, you would use the properties of the style object: 将该字符串分配给样式无效,您将使用样式对象的属性:
var rootDiv = document.createElement('div');
rootDiv.style.position = 'absolute';
rootDiv.style.right = '0px';
rootDiv.style.top = '0px';
var secondDiv = document.createElement('div');
secondDiv.style.margin = '10px';
secondDiv.style.padding = '1px';
secondDiv.style.webkitBoxShadow = 'rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px';
secondDiv.style.boxShadow = 'rgba(0, 0, 0, 0.298039) 0px 1px 4px -1px';
secondDiv.style.borderRadius = '2px';
secondDiv.style.backgroundColor = 'white';
secondDiv.style.width = '200px';
secondDiv.style.height = '150px';
However, I would suggest that you put the CSS in a style sheet, instead of adding all the styles directly to the element. 但是,我建议您将CSS放在样式表中,而不是将所有样式直接添加到元素中。
You can convert element to string by getting the element outerHTML
. 您可以通过获取元素outerHTML
将元素转换为字符串。
$(".gm-style").append(rootDiv.outerHTML);
Btw, if you use jQuery why you should append that as string? 顺便说一句,如果您使用jQuery,为什么要附加字符串呢? Using 使用
$(".gm-style").append(rootDiv);
will work. 将工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.