简体   繁体   English

如何使用html中的javascript获取iframe作为字符串并将其显示在div标签中?

[英]How to get the iframe as string and show it in the div tag using javascript in html?

As I have been trying to show the map in div tag in normal html. 正如我一直试图在普通html中以div标签显示地图。 My problem in Below code: 1. i have just called the getId() to show the map on div tag. 我在下面的代码中的问题:1.我刚刚调用了getId()以在div标签上显示地图。 2.i have used maps.google.com url with just passing the location name explicity 3.then get iframe as string , i have to show it in div 4. but after click on the button, nothing wil show on my web page. 2.我使用了maps.google.com网址,只是明确地传递了位置名称。3.然后将iframe用作字符串,我必须在div中显示它。4.单击按钮后,我的网页上没有任何显示。

i think my problem is, how to get the iframe and show it in div tag using javascript 我认为我的问题是,如何使用JavaScript获取iframe并将其显示在div标签中

<!doctype  html>
<head>
<script>

function getId() {
alert("function successfull"); 
        var addressHos = "sholinganallur" + "+India";    
        var url = "http://maps.google.com/maps?q=" + addressHos + "&output=embed";         
        var str = "<iframe width='500' height='300' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='" + url + "'></iframe>";
        document.getElementById("divmap").innerHTML = str;          
    }
</script>
</head>
<body>
<form method="get">
    <button id="lnkBtnMap" runat="server" onclick="return getId();">show</button>
<div id="divmap" >
</div>
</body>
</html>

Thanks In Advance 提前致谢

Ok, that was a tough one. 好的,那很艰难。 Took me a while to figure out 花了我一段时间才能弄清楚

You're using a <form> and a <button> within it. 您正在其中使用<form><button> In <form> , <button> acts as submit button and so refreshes the page. <form><button>充当提交按钮,因此刷新页面。 Because the page you were using were so small, you'd barelly notice it refreshed, expecially on localhost. 因为您使用的页面很小,所以您几乎会注意到它刷新了,尤其是在localhost上。

So prevent the click event to be processed by the form: 因此,请防止click事件由表单处理:

<button id="lnkBtnMap" runat="server" onclick="getId();return false;">Show</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM