javascript - 仅在单击链接时加载 iframe

标签 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>

最佳答案

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

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;
}

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

$('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 .

引用资料:

关于javascript - 仅在单击链接时加载 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23330696/

相关文章:

javascript - 在Require.js中,如果require()是一个函数,那么require.config()是如何存在的呢?

html - 将超链接从整个表格单元格更改为仅表格单元格中的文本

jquery - 为什么页面元素、动画、视差加载如此不稳定?

javascript - 在 Firefox 上将图像转换为 Base64 javascript

JavaScript - 获取有关浏览器的详细信息

javascript - 如何使 angular js 可拖动元素在平板电脑上工作(触摸)

javascript - html 5 音频文件播放完毕后重定向到新页面

html - 为什么 sidenav 中的文本会在特定屏幕宽度处消失?

html - 将文本定位在 html 元素的绝对中心

javascript - 移除 componentDidMount 上的 backgroundImage