2016-09-27 67 views
-2

为什么元素A不可见?看来appendChild()函数被忽略。我只能看到B元素。 (元素是可见的,如果我把它控制内部):(appendChild不能在Google地图中使用

var a = document.createElement('A'); 
a.style.width = '50px'; 
a.style.height = '50px'; 
a.style.cursor = 'pointer'; 
a.style.backgroundColor = 'rgba(10,20,30,1.0)'; 

var b = document.createElement('B'); 
b.style.width = '200px'; 
b.style.height = '200px'; 
b.style.backgroundColor = 'rgba(230,230,230,0.6)'; 

b.appendChild(a); 
map.controls[google.maps.ControlPosition.LEFT].push(b); 

回答

0

你的问题是,你必须同时显示在block水平

var a = document.createElement('A'); 
a.style.width = '50px'; 
a.style.height = '50px'; 
a.style.cursor = 'pointer'; 
a.style.backgroundColor = 'rgba(10,20,30,1.0)'; 
a.style.display = 'block'; //style block level 

var b = document.createElement('B'); 
b.style.width = '200px'; 
b.style.height = '200px'; 
b.style.backgroundColor = 'rgba(230,230,230,0.6)'; 
b.style.border = '1px solid black'; 
b.style.display = 'block'; //style block level 

document.body.appendChild(b); 
b.appendChild(a); 

https://jsfiddle.net/9rnzbuqh/

+0

非常感谢你,米奇! :) – thehorseisbrown

+0

@thehorseisbrown不客气 – Michelangelo

0

<a>元素是默认的内联元素,并且您不能设置内联元素的宽度或高度。您必须将其display更改为block

var a = document.createElement('A'); 
a.style.width = '50px'; 
a.style.height = '50px'; 
a.style.cursor = 'pointer'; 
a.style.backgroundColor = 'rgba(10,20,30,1.0)'; 
a.style.display = 'block'; 

var b = document.createElement('B'); 
b.style.width = '200px'; 
b.style.height = '200px'; 
b.style.backgroundColor = 'rgba(230,230,230,0.6)'; 
b.style.display = 'block'; 

b.appendChild(a); 
map.controls[google.maps.ControlPosition.LEFT].push(b); 
+0

这与我之前几分钟发布的答案有何不同? – Michelangelo

+0

谢谢Gothdo! – thehorseisbrown

0

我想清楚我在做什么。它没有设置显示样式,但我不知道为什么。

https://jsfiddle.net/9rnzbuqh/78/

var c = document.createElement('div'); 
c.style.height = '263px'; 
c.style.width = '350px'; 
c.style.marginLeft = '0px'; 
c.style.marginTop = '0px'; 
c.style.backgroundImage = "url(https://upload.wikimedia.org/wikipedia/en/5/5f/TomandJerryTitleCardc.jpg)"; 


var d = document.createElement('div'); 
d.style.background = 'rgba(230,230,230,0.6)'; 
d.style.paddingBottom = '70px'; 
d.style.paddingLeft = '40px'; 
d.style.paddingRight = '40px'; 
d.appendChild(c); 
document.body.appendChild(d); 

div { 
    width:400px; 
}