iframe源之间的Javascript按钮切换
单击按钮时,我想切换iframe源之间的Javascript按钮切换,javascript,iframe,Javascript,Iframe,单击按钮时,我想切换iframe的源。我试过的不管用 <input type="image" id="pluto" src="pluto.jpg" alt="pluto" width="150px" height="150px" style="margin-left: 45%;" onclick="function()"/> <script> document.getElementById('pluto').onclick = function() {
iframe
的源。我试过的不管用
<input type="image" id="pluto" src="pluto.jpg" alt="pluto" width="150px" height="150px" style="margin-left: 45%;" onclick="function()"/>
<script>
document.getElementById('pluto').onclick = function() {
{
var property = document.getElementById(iframe);
if (document.getElementById("something").src = "http://www.w3schools.com/";) {
document.getElementById("something").src = "http://agar.io/";
}
else {
document.getElementById("something").src = "http://www.w3schools.com/";
}
}
</script>
<iframe id='something' name="main" src="http://www.w3schools.com" style="border:none;width:100%;height:100%;margin-top:150px;"></iframe>
document.getElementById('pluto')。onclick=function(){
{
var属性=document.getElementById(iframe);
if(document.getElementById(“某物”).src=”http://www.w3schools.com/";) {
document.getElementById(“某物”).src=”http://agar.io/";
}
否则{
document.getElementById(“某物”).src=”http://www.w3schools.com/";
}
}
请帮助我。您的代码应该如下所示,请参阅JavaScript:
这里存在多个语法和语义问题。首先,您需要将正在使用的所有元素放入DOM中,因此脚本标记位于后面。选择iframe时,您希望使用其ID,而不是标记名。还需要使用您定义的变量,而不是反复搜索元素。然后,您的比较就是赋值nt-单方程符号和双方程符号产生了巨大的差异。最后,你的情况可能更复杂
if A then B
else A
这在文本中表示:如果URL是A,则切换到B;否则(读取URL是B)切换到A
这给我们留下了如下代码:
<input type="image" id="pluto" src="pluto.jpg" alt="pluto" width="150px" height="150px" style="margin-left: 45%;"/>
<iframe id='something' name="main" src="http://www.w3schools.com" style="border:none;width:100%;height:100%;margin-top:150px;"></iframe>
<script>
document.getElementById('pluto').onclick = function() {
{
var iframe = document.getElementById("something");
if (iframe.src == "http://www.w3schools.com/") {
iframe.src = "http://agar.io/";
}
else {
iframe.src = "http://www.w3schools.com/"
}
}
</script>
document.getElementById('pluto')。onclick=function(){
{
var iframe=document.getElementById(“某物”);
如果(iframe.src==”http://www.w3schools.com/") {
iframe.src=”http://agar.io/";
}
否则{
iframe.src=”http://www.w3schools.com/"
}
}
这里是一个使用jQuery和data属性的更短更智能的版本。它基本上将可选URL保存在一个属性中,并在按下按钮时切换它们
<input type="image" id="pluto" src="pluto.jpg" alt="pluto" width="150px" height="150px" style="margin-left: 45%;"/>
<iframe id='something' data-altsrc="http://agar.io/" name="main" src="http://www.w3schools.com" style="border:none;width:100%;height:100%;margin-top:150px;"></iframe>
<script>
$("#pluto").click(function() {
{
var iframe = $("#something");
var altsrc = iframe.data("altsrc");
iframe.data("altsrc", iframe.attr("src"));
iframe.attr("src", altsrc);
})
</script>
$(“#冥王星”)。单击(函数(){
{
变量iframe=$(“#某物”);
var altsrc=iframe.data(“altsrc”);
iframe.data(“altsrc”,iframe.attr(“src”);
iframe.attr(“src”,altsrc);
})
在脚本执行时,您的DOM可能还没有准备好。顺便说一句,什么是iframe
?您在哪里声明它?您比较错了-single=
符号是赋值运算符。此外,您的条件中不应该有分号。
<input type="image" id="pluto" src="pluto.jpg" alt="pluto" width="150px" height="150px" style="margin-left: 45%;"/>
<iframe id='something' data-altsrc="http://agar.io/" name="main" src="http://www.w3schools.com" style="border:none;width:100%;height:100%;margin-top:150px;"></iframe>
<script>
$("#pluto").click(function() {
{
var iframe = $("#something");
var altsrc = iframe.data("altsrc");
iframe.data("altsrc", iframe.attr("src"));
iframe.attr("src", altsrc);
})
</script>