Opening different iframe depending on different bu

2019-08-14 02:03发布

i am trying to open different iframe windows by assigning iframe src to a variable , when the page initially loads it should show only two buttons on the page , when i click on the button , an iframe should get loaded depending on which button is pressed , i tried doing something like this to create buttons and creating iframes , but it is not working

<button id="b1">Button 1</button>
<button id="b2">Button 2</button>
<iframe scrolling="no" src="whatnext">
</iframe>
<script>
var b1 = document.getElementById('b1'), b2 = document.getElementById('b2');
var x = "Hello!";
function showX() {
    var whatnext;
    if b1.onclick==true
        whatnext= "http://community.sitepoint.com/" style="border: 0px none; height: 1555px; margin-left: -116px; margin-top: -25px; width: 930px;"
        elseif b2.onclick=true
        whatnext= "http://community.sitepoint.com/" style="border: 0px none; height: 555px; margin-left: -116px; margin-top: -25px; width: 330px;"
}
</script>
and another piece of trial is
<button id="b1">Button 1</button>
<button id="b2">Button 2</button>
<iframe scrolling="no" src="x" style="border: 0px none; height: 1555px; margin-left: -116px; margin-top: -25px; width: 930px;">
</iframe>
<script>
var b1 = document.getElementById('b1'), b2 = document.getElementById('b2');
var x ;
function showX() {
    src="https:http://stackoverflow.com";
}
b1.onclick = function() {
    x = "http://stackoverflow.com;
    showX();
};
b2.onclick = function() {
    x = "www.sitepoint.com";
    showX();
};
</script>

2条回答
Melony?
2楼-- · 2019-08-14 02:32

Try this solution, at click on a button you load the webpage on a specific iframe, no jquery required.

JSBIN here http://jsbin.com/gebelatomiya/1/

<!doctype html>
<html>
<head>
<script>
function loadFrame (elm){
    var frame1 = document.getElementById('frame1');
    frame1.src = elm.dataset.src;
}
</script>
</head>
<body>
    <button id="b1" data-src="http://www.w3schools.com" onClick="loadFrame(this)">Button 1</button>
    <button id="b2" data-src="http://www.sony.com" onClick="loadFrame(this)">Button 2</button>
    <iframe id="frame1" scrolling="no"></iframe>
</body>
</html>
查看更多
聊天终结者
3楼-- · 2019-08-14 02:35

You just need to change the src attribute of the iframe on click. I added the src to the buttons usind the data attribute. Here is the example http://jsfiddle.net/8wLm42ns/2/ The HTML

    <button class="loadiframe" id="b1" data-src="http://jquery.com/" data-width="500" data-height="400">Button 1</button>
<button class="loadiframe" id="b2" data-src="http://sitepoint.com" data-width="650" data-height="350">Button 2</button>

<iframe id='frame' frameborder="0" scrolling="no" width="500" height="400">

jQuery

$('.loadiframe').on('click', function(){
    var src = $(this).data('src'),
        width = $(this).data('width'),
        height = $(this).data('height');
    $('#frame').css({
        width: width,
        height: height
    }).attr('src', src);
});

For javascript solution try this http://jsfiddle.net/8wLm42ns/3/

The HTML

<div class="loadiframe">
    <button id="b1" data-src="http://jquery.com/" data-width="500" data-height="400">Button 1</button>
    <button id="b2" data-src="http://sitepoint.com" data-width="650" data-height="350">Button 2</button>
</div>
<iframe id='frame' frameborder="0" scrolling="no" width="500" height="400">

javaScript - add in head section

function iframeChange() {
    var buttons = document.querySelector(".loadiframe"),
        iframe = document.getElementById('frame');

    buttons.addEventListener("click", function (e) {
        iframe.src = e.target.dataset.src;
        iframe.width = e.target.dataset.width;
        iframe.height = e.target.dataset.height;
    });
}
window.onload = iframeChange;
查看更多
登录 后发表回答