iframes的ifelse语句 [英] ifelse statement with iFrames

查看:62
本文介绍了iframes的ifelse语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iframe,我想做一个if条件,如果iframe源等于指定的页面,则div应该显示:

I have an iframe and i would like to make an if condition that if the iframe source is equal to a speciefied page a div should show:

示例

if(getElementById('iframe').src = 'someurl'){
getElementById('div').style.display. = 'block';
}

问题

我不是JS方面的专家,但是当我使用此脚本时,iframe会在加载主页时尽快自动加载"someurl",这是我不希望的,我希望在iframe源发生更改时显示div通过链接"someurl".链接工作正常,它更改了iframe src,但if语句不起作用.

I am not an expert in JS but when I use this script the iframe loads "someurl" automatically asoon as the main page is loaded, which I don't want, I want the div to be displayed when the iframe source changes to "someurl" via a link. The link is working fine and it changes the iframe src but the if statement isn't working.

推荐答案

  • = 分配
  • == 是一项相等性测试
  • === 是不带类型转换的相等性测试(通常更喜欢 ==
    • = is an assignment
    • == is an equality test
    • === is an equality test without type casting (and is generally prefered to ==
    • 使用 === ,而不是 = .

      然后,您需要正确引用 getElementById .它是 document 对象的方法,而不是全局方法.

      Then you need to reference getElementById correctly. It is a method of the document object, not a global.

      使用 document.getElementById 而不是 getElementById

      然后,您需要摆脱显示属性中尾随的.字符.

      Then, you'll need to get rid of the trailing . character from your display property.

      然后,假设您要在跟随框架中的链接时而不是仅在跟随当前页面中的链接时进行更新,则需要定期重新运行该功能以查看其是否已更改.

      Then, assuming you want to update this when a link in the frame is followed and not just when a link in the current page is followed, you need to rerun the function periodically to see if it has changed.

      setInterval(yourFunctionName, 5000); 
      

      然后,由于页面可能会偏离您要查找的URL,因此您需要添加else语句

      Then, since the page might move away from the URL you are looking for, you need to add an else statement

      function yourFunctionName () {
          var div = document.getElementById('div'); /* Really? Your unique ID for a specific div is 'div'?! */
          if(document.getElementById('iframe').src === 'someurl'){
              div.style.display = 'block';
          } else {
            div.style.display = 'none';
          }
      
      }
      

      这篇关于iframes的ifelse语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆