Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何分配不同的按钮以在iframe中加载不同的URL?_Javascript_Jquery_Html_Button_Iframe - Fatal编程技术网

Javascript 如何分配不同的按钮以在iframe中加载不同的URL?

Javascript 如何分配不同的按钮以在iframe中加载不同的URL?,javascript,jquery,html,button,iframe,Javascript,Jquery,Html,Button,Iframe,我被iFrames的问题缠住了 我有6个按钮,我想打开不同的网址时按下 按钮HTML代码: <div class="loadiframe"> <button id="b1" data-src="http://Site1.com/">Button 1</button> </div> <div class="loadiframe"> <button id="b2" data-src="http://Site2.com"

我被iFrames的问题缠住了

我有6个按钮,我想打开不同的网址时按下

按钮HTML代码:

<div class="loadiframe">
    <button id="b1" data-src="http://Site1.com/">Button 1</button>
</div>
<div class="loadiframe">
    <button id="b2" data-src="http://Site2.com">Button 2</button>
</div>
我最大的问题是class=“loadiframe”,因为按钮分散在整个页面上,我不能真正地把按钮放在一起,只调用一次

对此有什么建议或解决方法吗?

请更改此选项

var buttons = document.querySelector(".loadiframe"),
对此

var buttons = document.querySelectorAll(".loadiframe button"),
querySelector:返回文档中的第一个元素(使用 深度优先(文档节点的预顺序遍历)匹配 指定的选择器组

querySelectorAll:返回文档中与指定选择器组匹配的元素列表(使用文档节点的深度优先顺序遍历)

下面是我的jquery解决方案:

$('.loadiframe >button').click(function(){
    $('#frame').attr('src', $(this).data('src'));
})
请换这个

var buttons = document.querySelector(".loadiframe"),
对此

var buttons = document.querySelectorAll(".loadiframe button"),
querySelector:返回文档中的第一个元素(使用 深度优先(文档节点的预顺序遍历)匹配 指定的选择器组

querySelectorAll:返回文档中与指定选择器组匹配的元素列表(使用文档节点的深度优先顺序遍历)

下面是我的jquery解决方案:

$('.loadiframe >button').click(function(){
    $('#frame').attr('src', $(this).data('src'));
})

您可以在按钮上使用内联单击事件:

<iframe name="frame" id="frame" src="http://site1.com"></iframe>

<button type="button" onClick="document.getElementById('frame').src='http://site2.com'">Button #1</button>
<button type="button" onClick="document.getElementById('frame').src='http://site3.com'">Button #2</button>
<button type="button" onClick="document.getElementById('frame').src='http://site4.com'">Button #3</button>

不需要JavaScript。

您可以在按钮上使用内联单击事件:

<iframe name="frame" id="frame" src="http://site1.com"></iframe>

<button type="button" onClick="document.getElementById('frame').src='http://site2.com'">Button #1</button>
<button type="button" onClick="document.getElementById('frame').src='http://site3.com'">Button #2</button>
<button type="button" onClick="document.getElementById('frame').src='http://site4.com'">Button #3</button>

不需要JavaScript。

很抱歉,忘记在上添加结束标记,但问题不是这样。很抱歉,忘记在上添加结束标记,但问题不是这样。不,仍然没有加载。当var buttons=document.querySelector(“.loadiframe”)时,它加载了第一个按钮,但添加了所有按钮后,它也不会加载。我以前试过,我不知道x(@KillahHerb你试过了吗?)我想避免使用jquery,但感谢@zep_fan帮我找到了方法。没有,仍然没有加载。使用var buttons=document.querySelector(“.loadiframe”),它加载了第一个按钮,但添加了所有按钮后,它也没有加载。我以前试过,我不知道x(@KillahHerb你试过jquery了吗?)我想避免使用jquery,但是谢谢@zep_fan帮我找到了路。谢谢你想得太多了