简体   繁体   English

通过一次onClick触发多个事件

[英]triggering multiple events with single onClick

I have a iframe on my web page. 我的网页上有一个iframe On anchor click a pdf file opens in the iframe . 在锚点上单击,将在iframe打开一个pdf文件。 I have provided button by clicking which I close/display:none the iframe . 我已通过单击关闭/显示的按钮来提供:无iframe I want the iframe to show again when the anchor is clicked. 我希望单击锚点时再次显示iframe The click of the anchor should show the iframe and open the pdf in that iframe . 单击锚点应显示iframe并在该iframe打开pdf。 Please help me how to do that. 请帮我怎么做。

I have tried the following: 我尝试了以下方法:

function CloseWindow() {
var ifr = document.getElementById("ifrlyric") 
ifr.style.display='none' }

function OpenWindow() {
var ifro = document.getElememtById("ifrlyric")
if (ifro.style.display=='none') { ifro.style.display='block' }   }

<a href="#" onClick=" window.lyrics.location='mypage.pdf';  ifrlyric.style.display=block;   return false;">Mypage</a>

The anchor is opening the pdf in the iframe first time and the click button hides it. 锚点是第一次在iframe打开pdf,然后点击按钮将其隐藏。 But again when the anchor is clicked the iframe is not showing. 但是再次单击锚点时, iframe不会显示。

Try to do a third function such as 尝试执行第三个功能,例如

function toggleState() {
    if(document.getElementById('ifrlyric').style.display == 'none') {
        window.lyrics.location='mypage.pdf'; // And I suggest moving this to OpenWindow
        OpenWindow ();
    } else {
        CloseWindow ();
    }
}

And then call it in your onClick : 然后在您的onClick中调用它:

<a href="#" onClick="toggleState();">Mypage</a>

EDIT 编辑

Re-reading your code I can see a typo, you wrote getElememtById instead of getElementById 重新阅读您的代码,我可以看到一个错字,您写了getElememtById而不是getElementById

The function "OpenWindow" has the only mission to show the iframe, so I suggest to simply: 函数“ OpenWindow”具有显示iframe的唯一任务,因此我建议简单地:

 function OpenWindow() {
    var ifro = document.getElememtById("ifrlyric")
     ifro.style.display='block'    }

Since you tagged jquery, I'll say just use the jquery toggle function: 既然您标记了jquery,我就说只使用jquery切换功能:

<a href="#" onClick="setLocation();">Mypage</a>

<script>
    function setLocation() {
        window.lyrics.location='mypage.pdf';
        $('#ifrlyric').toggle();
</script>

You didn't post enough code for me to know if this is exactly right, but it should at least be pretty close. 您没有发布足够的代码让我知道这是否完全正确,但是至少应该非常接近。

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

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