我希望在我的HTML页面中有一个文本框,还有一个iframe
。当用户在文本框中键入URL并单击“提交”时,iframe
将导航到该页面。有可能吗?
答案 0 :(得分:1)
使用jquery单行代码非常简单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="application/javascript">
function navigate() {
$('#iframe1').attr('src', $('#ifrmsite').val());
return false;
}
</script></head>
<body>
Enter website url below:<br/>
<form onSubmit="return navigate();" method="get" action="">
<input type="text" value="http://www.w3schools.com/" name="ifrmSite" id="ifrmsite"/>
<input type="submit" value="Submit">
</form>
<br /><br />
<iframe name="iframe1" id="iframe1" src="" width="600" height="700" scrolling="auto">
</iframe>
</body>
</html>
答案 1 :(得分:0)
如果您不想使用jquery:
<script type="application/javascript">
function submitStuff(){
document.getElementById('iFrame').src = 'http://' + document.theForm.url.value;
return false ;
}
</script>
<form name="theForm" method="post" onsubmit="return submitStuff();">
<input name="url" value="">
<input type="submit" name="submitButton" value="Submit">
</form>
<iframe id="iFrame" src="http://www.default.com"></iframe>