繁体   English   中英

仅在点击链接时加载iframe

[英]only load iframe when link is clicked

如何一次显示一个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>

我建议的一种方法是先用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小提琴演示

一种替代方法是在页面上只有一个<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小提琴演示

一个迟来的编辑(使用纯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小提琴演示

上述方法的最终迭代(仍然是纯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小提琴演示

参考文献:

您可以尝试将.active类添加到.active的iframe中

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

看这个例子。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM