【问题标题】:call a function using resize()使用 resize() 调用函数
【发布时间】:2019-02-22 05:26:04
【问题描述】:

我想添加事件侦听器以在我的窗口调整大小时调用一个函数,但不知何故它不起作用!我尝试了这些代码,但没有奏效!

$(function(){$(window).resize(titleResize);});

$(function(){$(window).resize(titleResize());});

$(window).resize(titleResize);

$(window).resize(titleResize());

有什么想法吗?

顺便说一句,这是我的 titleResize 函数:

function titleResize() {
        maxHeight = 0;
        $("div.lae-entry-text-wrap > h3").each(function(){
            titleHeight = $(this).height();
            if (titleHeight > maxHeight){
                maxHeight = titleHeight;
            }
        });
        $("div.lae-entry-text-wrap > h3").css("height", maxHeight);
    }

【问题讨论】:

  • 检查控制台是否有错误
  • 控制台没有错误,好像根本就没有事件监听!
  • 可能浏览器缓存了旧代码,所以按ctrl+F5清除缓存
  • @Mohammad 运气不好!

标签: jquery


【解决方案1】:

尝试关注

$(window).resize(function(){
alert('Window Is Resized')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

【讨论】:

  • 这会起作用,但是当我调用我的函数时,它不会!
  • 函数titleResize() { maxHeight = 0; $("div.lae-entry-text-wrap > h3").each(function(){ titleHeight = $(this).height(); if (titleHeight > maxHeight){ maxHeight = titleHeight; } }); $("div.lae-entry-text-wrap > h3").css("height", maxHeight); }
  • 什么意思$("div.lae-entry-text-wrap > h3")
【解决方案2】:

在关注Script 你的函数正在调用

$(window).resize(function(){
debugger
titleResize();
});
   function titleResize() {
   alert('Function titleResize() Called');
   maxHeight = 0; 
   $("div.lae-entry-text-wrap > h3").each(function(){ 
   titleHeight = $(this).height(); 
   if (titleHeight > maxHeight)
   {
   maxHeight = titleHeight; 
   } 
   });
   $("div.lae-entry-text-wrap > h3").css("height", maxHeight); 
   }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

【讨论】:

    【解决方案3】:

    这样试试

    $(window).on('resize', function(){ //Your code });
    

    $(window).on('resize', function(){
        console.log('resize')
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    【讨论】:

      【解决方案4】:

      我也有同样的问题,你可以使用这种类型的代码来解决你的错误

       $(window).resize( () => {
              let Width = $(window).width();
              if (Width > 910) {
                  $(".navbar1").hide();
               }
          })
      

      【讨论】:

        猜你喜欢
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-07
        • 1970-01-01
        • 2011-11-14
        • 2011-11-06
        • 1970-01-01
        相关资源
        最近更新 更多