问题
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