0

我想要这段代码做的是显示一个名为 Map 的按钮。在此之上,一些文本应为“选择显示器”。然后,一旦单击“地图”按钮,它就会通过标签显示谷歌地图图像。

<html>
<head>

<title>Random</title>

</head>
<body>


<p align="center"style="font-family:verdana;"id="googlemap">
Choose a Display
</p>

<script>
function mapFunction()
{
x=document.getElementById("googlemap");  // Find the element
x.src="<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0"     marginwidth="0" https://www.google.com.au/maps?f=d&amp;source=s_d&amp;saddr=23+Burgundy+St,+Carseldine+QLD&amp;daddr=90+Gilbert+Rd,+Grange+QLD+to:Narrowneck+Court+Surfers+Paradise,+204+Ferny+Ave,+Surfers+Paradise+QLD&amp;hl=en&amp;geocode=FfOeXv4d6qweCSn9XcBQSP2TazGVQ_0hH3aSYA%3BFRKQXf4d5_QeCSlRy4j_0lmRazEdtrkd8CRD0Q%3BFWXtVP4dWCUlCSGK90q-pKXOZCmv-18SewWRazGK90q-pKXOZA&amp;aq=0&amp;oq=Narr&amp;sll=-27.422699,153.02411&amp;sspn=0.004819,0.008272&amp;t=h&amp;mra=ls&amp;ie=UTF8&amp;ll=-27.555764,153.208466&amp;spn=0.584401,0.878906&amp;z=10&amp;output=embed"></iframe>;    // Change the content
}
</script>

<button type="button" onclick="mapFunction()">Map</button>


</body>
</html>
4

4 回答 4

5

我想你的意思是innerHTML

var x = document.getElementById("googlemap");  // Find the element
x.innerHTML = '<iframe src="url here" ...></iframe>';

由于 HTML 包含",您可以使用单引号对它们进行转义\或仅将整个字符串包含在单引号'中。

另外,您忘记srciframe.

于 2013-05-17T06:17:08.993 回答
0

这是因为,一是你需要使用innerHTML,二是你需要在设置时不要关闭引号innerHTML

x.innerHTML='<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://www.google.com.au/maps?f=d&amp;source=s_d&amp;saddr=23+Burgundy+St,+Carseldine+QLD&amp;daddr=90+Gilbert+Rd,+Grange+QLD+to:Narrowneck+Court+Surfers+Paradise,+204+Ferny+Ave,+Surfers+Paradise+QLD&amp;hl=en&amp;geocode=FfOeXv4d6qweCSn9XcBQSP2TazGVQ_0hH3aSYA%3BFRKQXf4d5_QeCSlRy4j_0lmRazEdtrkd8CRD0Q%3BFWXtVP4dWCUlCSGK90q-pKXOZCmv-18SewWRazGK90q-pKXOZA&amp;aq=0&amp;oq=Narr&amp;sll=-27.422699,153.02411&amp;sspn=0.004819,0.008272&amp;t=h&amp;mra=ls&amp;ie=UTF8&amp;ll=-27.555764,153.208466&amp;spn=0.584401,0.878906&amp;z=10&amp;output=embed"></iframe>';
于 2013-05-17T06:23:40.637 回答
0

两件事情:

  • 您不想更改srcx,而是尝试修改innerHTML.

  • 您的 iframe 标记格式错误(URL 应以前缀为前缀,src="并且没有 a marginwidthormarginheight属性之类的东西)并且未正确包含在字符串中(所有双引号都应转义\"like this\"

所以,这应该工作:

function mapFunction()
{
    x=document.getElementById("googlemap");  // Find the element
    x.innerHTML = "<iframe width=\"640\" height=\"480\" frameborder=\"0\" scrolling=\"no\" style=\"margin-height:0;margin-width:0;\" src=\"https://www.google.com.au/maps?f=d&amp;source=s_d&amp;saddr=23+Burgundy+St,+Carseldine+QLD&amp;daddr=90+Gilbert+Rd,+Grange+QLD+to:Narrowneck+Court+Surfers+Paradise,+204+Ferny+Ave,+Surfers+Paradise+QLD&amp;hl=en&amp;geocode=FfOeXv4d6qweCSn9XcBQSP2TazGVQ_0hH3aSYA%3BFRKQXf4d5_QeCSlRy4j_0lmRazEdtrkd8CRD0Q%3BFWXtVP4dWCUlCSGK90q-pKXOZCmv-18SewWRazGK90q-pKXOZA&amp;aq=0&amp;oq=Narr&amp;sll=-27.422699,153.02411&amp;sspn=0.004819,0.008272&amp;t=h&amp;mra=ls&amp;ie=UTF8&amp;ll=-27.555764,153.208466&amp;spn=0.584401,0.878906&amp;z=10&amp;output=embed\"></iframe>";    // Change the content
}
于 2013-05-17T06:20:36.100 回答
0

p元素永远不能有 src 属性。或者您使用空 src 值创建 iframe / 或者使用x.innerHTML将 iframe 写入其中。

正如其他人之前所说,您需要“转义引号”

于 2013-05-17T06:21:36.400 回答