gpt4 book ai didi

javascript - 如何: Change target URL in iFrame using input text+submit fields & JavaScript

转载 作者:行者123 更新时间:2023-12-03 03:40:28 25 4
gpt4 key购买 nike

因此,我在工作期间随机编写了一个网页,并且遇到了一个问题,即我无法可靠地将 URL 设置为默认情况下已设置为加载 http://getbootstrap.com 的 iframe 的“src”。任何更改 URL 的尝试都会以默认值重新加载 iframe。

我尝试应用在此站点上找到的先前记录的解决方案,但遗憾的是没有取得任何成功。

这是我正在使用的一些代码:

HTML5

<form name="browser" action="" method="post">
<div class="container-fluid">
<div class="row">
<input style="color:#000000;" id="addressBar" class="col-md-7" type="text" />
<!--<input id="goBtn" class="btn btn-default col-md-1" type="submit" onclick="addressGo()" value="Go" />-->
<input id="goBtn" class="btn btn-default col-md-1" type="submit" onclick="testCall()" value="Go" />
</div>
</div>
</form>
<div class="container-fluid">
<div class="row">
<iframe id="browserFrame" class="col-md-8" src="http://getbootstrap.com" frameborder="0" >
<p>Something went wrong...target website doesn't allow iframes to be used.</p>
</iframe>
...
</div>
</div>
...
<script src="..\bootstrap-3.3.7-dist\js\bootstrap.min.js"></script>
<script src="..\JavaScript\Custom.js"></script>

JavaScript

function addressGo () { //Function to browse to inputted address
var addressBar = document.getElementById("addressBar"); //Declare Address Bar variable
var browserFrame = document.getElementById("browserFrame"); //Declare iframe variable

browserFrame.src=addressBar; //Set iframe URL as input from Address Bar
}

function testCall () { //Generic test function (to be re-written as needed)
var addressBar = document.getElementById("addressBar"); //Declare Address Bar variable
var browserFrame = document.getElementById("browserFrame"); //Declare iframe variable

alert("Browser Frame: "+browserFrame+"\nAddress Bar: "+addressBar.);
}

现在,经过测试,我收到了测试“警报”,其中包含以下消息:

警告:

Browser Frame: [object HTMLIFrameElement]
Address Bar: [object HTMLInputElement]

我觉得我已经接近解决方案了,但我对编码有点缺乏练习。任何想法表示赞赏。 :)

问候,

~舞蹈链接

最佳答案

<input id="goBtn" class="btn btn-default col-md-1" type="submit" onclick="return addressGo()" value="Go" />

function addressGo () { //Function to browse to inputted address
var addressBar = document.getElementById("addressBar").value; //Declare Address Bar variable
var browserFrame = document.getElementById("browserFrame"); //Declare iframe variable
browserFrame.src=addressBar; //Set iframe URL as input from Address Bar
return false;
}

提交表单时将重新加载整个页面。在您的情况下,最好将 go 按钮放在表单之外,以便不需要 return falseonclick="return addressGo()".

关于javascript - 如何: Change target URL in iFrame using input text+submit fields & JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45645767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com