javascript - 选择默认类 'active' 以打开 iframe

标签 javascript css iframe

每次用户单击按钮时,我都有这段代码打开不同的 iframe。但是我注意到,当有人加载页面时,没有任何“默认”iframe 可以打开,因此 iframe 看起来是黑色和空的。有没有一种方法可以编辑代码以让类在默认情况下处于事件状态?

$(document).ready(function() {
  // Catch all clicks on a link with the class 'link'
  $('.link').click(function(e) {

  // Stop the link being followed:
  e.preventDefault();

  // Get the div to be shown:
  var content = $(this).attr('rel');

  // Remove any active classes:
  $('.active').removeClass('active');

  // Add the 'active' class to this link:
  $(this).addClass('active');

  // Hide all the content:
  $('.content').hide();

  // Show the requested content:
  $('#' + content).show();
});

最佳答案

您可以尝试这样的操作,它会尝试在页面加载时显示您的第一个链接。我无法测试,因为我没有你所有的代码,但希望这接近你正在寻找的东西。

  $(document).ready(function() {

    showLink($('.link').first());

    // Catch all clicks on a link with the class 'link'
    $('.link').click(function(e) {
        e.preventDefault();
        showLink($(this));    
    });

    function showLink(ele){

          // Get the div to be shown:
          var content = ele.attr('rel');

          // Remove any active classes:
          $('.active').removeClass('active');

          // Add the 'active' class to this link:
          ele.addClass('active');

          // Hide all the content:
          $('.content').hide();

          // Show the requested content:
          $('#' + content).show();
    }
});

关于javascript - 选择默认类 'active' 以打开 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34775886/

相关文章:

jquery - 在大屏幕上按行显示 flex 元素,但在小屏幕上垂直堆叠

javascript - Chrome 在我的 iframe 中限制了诸如 requestAnimationFrame 之类的东西

Facebook:页面选项卡的新 IFrame Canvas 宽度低于 520 像素?

javascript - iframe 加载是否会阻止 javascript 执行?

javascript - 如何在两个列表框之间移动元素?

css - 无法使用 WebStorm 设置 Compass

JavaScript:向可能不存在的对象添加嵌套属性

javascript - 使谷歌地图跨越长页面

javascript - 在另一个文本框或 div 中写入内容后清除

javascript - FabricJS:将 SVG 添加到 Canvas 但在导出时不可见