English 中文(简体)
重新调整窗口大小时, 无法重新运行脚本
原标题:Having trouble getting a script to run again when resizing the window
  • 时间:2012-05-28 05:15:41
  •  标签:
  • jquery

我不知道我在做什么 做得足够好, 才能弄清楚为什么在调整窗口的大小时, 高度没有被重新计算。 另外,我希望计算方法能对三所学校的每个学校进行分流。

任何帮助请见:http://jsfiddle.net/joshnh/kmwmf/

$(window).resize( function() {

    var $school = $( .content ul ),
        $campus = $( .content ul p ),
        campusHeight = 0;

    $school.each( function() {
        $campus.each( function() {
            if($(this).height() > campusHeight) {
                campusHeight = $(this).height();
            }
        });
    });

    $campus.height(campusHeight);        
    console.log(campusHeight);

}).trigger( resize );
最佳回答

我最后自己解决了这个问题:http://jsfiddle.net/joshnh/kmwmf/7,

在测量高度前删除内线样式属性, 以确保当调整窗口大小时代码工作正确 。

$(window).resize( function() {

    var $school = $( .content ul ); 

    $school.each( function() {
        var $campus = $(this).find( p ),
            campusHeight = 0;

        $campus.each( function() {
            $(this).removeAttr( style );
            if($(this).height() > campusHeight) {
                campusHeight = $(this).height();
            } 
        });

        $campus.height(campusHeight);
    });   

}).trigger( resize );
问题回答

以下是您的代码正在做的 :

var $school = $( .content ul ),    // select all school ul elements
    $campus = $( .content ul p ),  // select ALL campus paragraphs
    campusHeight = 0;

$school.each( function() {         // iterate over the schools
    $campus.each( function() {     // iterate over ALL campuses, not just
                                   // the ones for the current school

以下应分别计算每一所学校:

$(window).resize( function() {

    $( .content ul ).each( function() { // iterate over the school ul elements
        var campusHeight = 0,
            $campus = $( p , this);     // get campuses for current school
        $campus.each( function() {      // iterate over current campuses
            if($(this).height() > campusHeight) {
                campusHeight = $(this).height();
            }
        });
        $campus.height(campusHeight);   // set height for campuses
        console.log(campusHeight);      // in current school
    });    

}).trigger( resize );

在我的代码中,我实现了 $schools 变量的升级,因为您可以直接将外部 .each () 直接放到初始的 $(.content ul) 上。 同样,您也可以通过将内部 结尾处的高度设置代码连锁起来来消除 $campus 变量。 each :

        $( p , this).each( function() {
            if($(this).height() > campusHeight) {
                campusHeight = $(this).height();
            }
        }).height(campusHeight);

以下是您可以做的, 将每个学校的校区分别隔开来评估身高:

$(window).resize( function() {

  var $school = $( .content ul );

$school.each( function() {
    $campus = $(this).find( p );
    campusHeight = 0;
    $campus.each( function() {
        if($(this).height() > campusHeight) {
            campusHeight = $(this).height();
        }
    });
     $campus.height(campusHeight);
});



  console.log(campusHeight);

}).trigger( resize );

这只分别调整校区大小。 要重新计算高度, 请参看此编辑 :

 $(window).resize( function() {

    var $school = $( .content ul );

    $school.each( function() {
    $campus = $(this).find( p );
    campusHeight = 0;
    $campus.css( height ,   ); //forces to remove height to recalculate new height

    $campus.each( function() {
        if($(this).height() > campusHeight) {
            campusHeight = $(this).height();
        }
    });
     $campus.height(campusHeight);
});



  console.log(campusHeight);

}).trigger( resize );




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签