jQuery while循环导致浏览器崩溃

jQuery while loop makes browser crash

本文关键字:浏览器 崩溃 while 循环 jQuery      更新时间:2023-09-26

我的网页中有以下while循环:

<script>
$(document).ready(function(){
    var height = $("#quote").height();
    //alert("Toegestane hoogte: 500px'nHuidige hoogte: "+height+"px");
    while (height > 500) {
        var fontSize = $('p', $('#quote')).css('font-size').split('px')[0];
        var fontInt = parseInt(fontSize) - 1;
        fontInt = fontInt + 'px';
        $('p', $('#quote')).css('font-size', fontInt);
    }
});

不知何故,当我加载页面时,浏览器一直在加载,最终它崩溃了…

var height在循环过程中永远不会增加,这会导致无限循环。

var height在循环中永远不会增加,从而导致无限循环

检查是否使用if语句

<script>
$(document).ready(function(){
    var height = $("#quote").height();
    //alert("Toegestane hoogte: 500px'nHuidige hoogte: "+height+"px");
    if (height > 500) {
        var fontSize = $('p', $('#quote')).css('font-size').split('px')[0];
        var fontInt = parseInt(fontSize) - 1;
        fontInt = fontInt + 'px';
        $('p', $('#quote')).css('font-size', fontInt);
    }
});