我希望此代码执行的操作是显示一个名为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&source=s_d&saddr=23+Burgundy+St,+Carseldine+QLD&daddr=90+Gilbert+Rd,+Grange+QLD+to:Narrowneck+Court+Surfers+Paradise,+204+Ferny+Ave,+Surfers+Paradise+QLD&hl=en&geocode=FfOeXv4d6qweCSn9XcBQSP2TazGVQ_0hH3aSYA%3BFRKQXf4d5_QeCSlRy4j_0lmRazEdtrkd8CRD0Q%3BFWXtVP4dWCUlCSGK90q-pKXOZCmv-18SewWRazGK90q-pKXOZA&aq=0&oq=Narr&sll=-27.422699,153.02411&sspn=0.004819,0.008272&t=h&mra=ls&ie=UTF8&ll=-27.555764,153.208466&spn=0.584401,0.878906&z=10&output=embed"></iframe>; // Change the content
}
</script>
<button type="button" onclick="mapFunction()">Map</button>
</body>
</html>
答案 0 :(得分:5)
我认为你的意思是innerHTML
var x = document.getElementById("googlemap"); // Find the element
x.innerHTML = '<iframe src="url here" ...></iframe>';
由于HTML包含"
,您可以使用\
转义它们,也可以使用单引号'
包裹整个字符串。
另外,您忘记了src
的{{1}}。
答案 1 :(得分:0)
两件事:
您不想更改x的src
,而是尝试修改innerHTML
。
您的iframe标记格式错误(网址应以src="
为前缀,且不存在marginwidth
或marginheight
属性)并且未正确包含在字符串中(所有dublequotes都应该被转义\"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&source=s_d&saddr=23+Burgundy+St,+Carseldine+QLD&daddr=90+Gilbert+Rd,+Grange+QLD+to:Narrowneck+Court+Surfers+Paradise,+204+Ferny+Ave,+Surfers+Paradise+QLD&hl=en&geocode=FfOeXv4d6qweCSn9XcBQSP2TazGVQ_0hH3aSYA%3BFRKQXf4d5_QeCSlRy4j_0lmRazEdtrkd8CRD0Q%3BFWXtVP4dWCUlCSGK90q-pKXOZCmv-18SewWRazGK90q-pKXOZA&aq=0&oq=Narr&sll=-27.422699,153.02411&sspn=0.004819,0.008272&t=h&mra=ls&ie=UTF8&ll=-27.555764,153.208466&spn=0.584401,0.878906&z=10&output=embed\"></iframe>"; // Change the content
}
答案 2 :(得分:0)
p 元素永远不会有src属性。或者您使用空src值创建iframe /或者使用 x.innerHTML 来编写iframe。
正如其他人之前所说,你需要“逃避引号”
答案 3 :(得分: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&source=s_d&saddr=23+Burgundy+St,+Carseldine+QLD&daddr=90+Gilbert+Rd,+Grange+QLD+to:Narrowneck+Court+Surfers+Paradise,+204+Ferny+Ave,+Surfers+Paradise+QLD&hl=en&geocode=FfOeXv4d6qweCSn9XcBQSP2TazGVQ_0hH3aSYA%3BFRKQXf4d5_QeCSlRy4j_0lmRazEdtrkd8CRD0Q%3BFWXtVP4dWCUlCSGK90q-pKXOZCmv-18SewWRazGK90q-pKXOZA&aq=0&oq=Narr&sll=-27.422699,153.02411&sspn=0.004819,0.008272&t=h&mra=ls&ie=UTF8&ll=-27.555764,153.208466&spn=0.584401,0.878906&z=10&output=embed"></iframe>';