Targeting iframe src from user input in javascript form

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 17:18:19

问题


I'm trying to target an iframe's source from a javascript form. So if someone typed in http://www.reddit.com the iframe's source would change to reddit.com I've tried a few things but you can't put a script tag within src, so is there any way to do this with javascript, or would i need to make a php echo function?

<iframe  src="http://www.stachoverflow.com/"></iframe>

回答1:


Do you mean something like this?

<input type="url" placeholder="http://example.com/" /><button id="urlclick">Go</button>
<iframe id="frm"></iframe>

<script type="text/javascript">
  document.getElementById('urlclick').onclick = function() {
    document.getElementById('frm').src = this.previousSibling.value;
    return false;
  }
</script>



回答2:


By using jquery we can do like this..

<input type="text" id="address">
<button onclick="get_url()"></button>
<iframe id="frm"></iframe>

<script>
function get_url()
{
var adr=$('#address').val();
$('#frm').attr('src', url);
}
</script>


来源:https://stackoverflow.com/questions/14007702/targeting-iframe-src-from-user-input-in-javascript-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!