仅在单击链接时加载iframe

时间:2014-04-28 00:23:50

标签: javascript html css href

如何一次显示一个iframe?所以我有一个索引网页,导航栏中有链接到iframe,但我希望iframe只在点击链接时显示?现在他们都在显示。这是一个JavaScript解决方案吗?我是编码的新手,所以如果是这样的话,我可能更容易不使用iframe,只是设置不同的页面并定位id。

这是我的代码: http://jsfiddle.net/r2mmb/

HTML

 <header>
<div id="logo">
<img src="../../assets/images/logo.png" alt="">
</div>
<nav>
<ul>
<li><a href="iframetoursprices.html" target="toursprices">
    TOURS,PRICES &amp; STANDARD FLIGHTS</a></li>
<li><a href="#">MEET THE STAFF</a></li>
<li><a href="iframegallery.html" target="gallery">Gallery</a></li>
</ul>
</nav>
    </header>

 <div class="mainInfo">
<iframe src="iframetoursprices.html" name="toursprices"></iframe>
<iframe src="iframegallery.html" name="gallery"></iframe>
</div>

2 个答案:

答案 0 :(得分:1)

我建议的一种方法是先用CSS隐藏所有<iframe>元素:

iframe {
    display: none;
}

使用class创建一个CSS规则来显示该元素:

iframe.inUse {
    display: block;
}

使用jQuery:

// binds a click handler to all 'a' elements with a 'target' attribute:
$('a[target]').click(function(){
    // creates a reference to the clicked-link:
    var clickedEl = this;

    // gets all 'iframe' elements in the document:
    $('iframe')
        // removes the 'inUse' class from all of them:
        .removeClass('inUse')
        // filters the collection of 'iframe' elements:
        .filter(function(){
            // we keep only the 'iframe' whose 'name' attribute
            // is equal to the 'name' attribute of the clicked 'a':
            return this.name === clickedEl.target;
        // and we add the 'inUse' class to that iframe element:
        }).addClass('inUse');
});

JS Fiddle demo

另一种方法是在页面上只有一个<iframe>元素,用CSS隐藏它:

iframe {
    display: none;
}

使用以下jQuery将内容加载到单个<iframe>中:

$('a[target]').click(function(e){
    // prevents the default click-behaviour of the link:
    e.preventDefault();

    // finds the 'iframe' element with the name of 'showContent':
    $('iframe[name="showContent"]')
        // sets its 'src' property equal to the 'href' property of the clicked link:
        .prop('src', this.href)
        // shows the 'iframe':
        .show();
});

JS Fiddle demo

迟来的编辑,使用纯JavaScript,根据有些过时的解释,jQuery无法使用:

function contentToIframe () {
    // getting a reference to the 'iframe' element whose 'name' attribute
    // is equal to the clicked element's 'target' attribute, using CSS
    // notation available under document.querySelector() (which returns
    // only the first element that matches the selector):
    var iframe = document.querySelector('iframe[name="' + this.target + '"]'),
        // getting a reference to the current 'display' (inline) style of
        // the 'iframe' (found above):
        curDisplay = iframe.style.display;

    // setting the 'src' property of the 'iframe' equal to the 'href'
    // of the clicked link:
    iframe.src = this.href;

    // if the 'iframe' doesn't have a set 'display' style-property, or
    // it is not set to 'block':
    if (!curDisplay || curDisplay !== 'block') {
        // we set it to 'block' to make it visible:
        iframe.style.display = 'block';
    }
}

// getting all the 'a' elements in the document that also have a 'target'
// attribute:
var links = document.querySelectorAll('a[target]');

// iterating over those link elements:
for (var i = 0, len = links.length; i < len; i++) {
    // binding an event-handler to deal with the click event,
    // which executes the function:
    links[i].addEventListener('click', contentToIframe);
}

JS Fiddle demo

上述(仍然是纯JavaScript)方法的最终迭代,现在还允许在单击链接加载内容时滚动到<iframe>

function contentToIframe() {
    var iframe = document.querySelector('iframe[name="' + this.target + '"]'),
        curDisplay = iframe.style.display,
        // getting a reference to the current position of the 'iframe' element:
        position = iframe.getBoundingClientRect();
    iframe.src = this.href;
    if (!curDisplay || curDisplay !== 'block') {
        iframe.style.display = 'block';
        // if the 'iframe' wasn't visible it's position would have been 0,0;
        // so once we've made it visible we re-get its new (now visible)
        // coordinates:
        position = iframe.getBoundingClientRect();
    }

    // force the window to scrollTo the current position (x,y) of the 'iframe':
    window.scrollTo(position.left, position.top);
}

var links = document.querySelectorAll('a[target]');

for (var i = 0, len = links.length; i < len; i++) {
    links[i].addEventListener('click', contentToIframe);
}

JS Fiddle demo

参考文献:

答案 1 :(得分:0)

您可以尝试将班级.active添加到您组合的iframe

并将onclick事件添加到您的菜单中。

看看这个例子。

http://jsfiddle.net/r2mmb/1/