带有文本换行符的 JavaScript 语法错误

标签 javascript css

在控制台上运行 html 检查时,当拾取包含新行的某些文本时会出现错误。 我尝试使用 white-space: pre-lineCSS 中更正它,但错误仍然存​​在。 下面是没有错误的完整代码:

<script type="text/javascript" async="">
        (function () {
    var Message;
    Message = function (arg) {
        this.text = arg.text, this.message_side = arg.message_side;
        this.draw = function (_this) {
            return function () {
                var $message;
                $message = $($('.message_template').clone().html());
                $message.addClass(_this.message_side).find('.text').html(_this.text);
                $('.messages').append($message);
                return setTimeout(function () {
                    return $message.addClass('appeared');
                }, 0);
            };
        }(this);
        return this;
    };
    $(function () {
        var getMessageText, message_side, sendMessage;
        message_side = 'left';
        getMessageText = function () {
            var $message_input;
            $message_input = $('.message_input');
            return $message_input.val();
        };
        sendMessage = function (text) {
            var $messages, message;
            if (text.trim() === '') {
                return;
            }
            $('.message_input').val('');
            $messages = $('.messages');
            message_side = message_side === 'left' ? 'right' : 'left';
            message = new Message({
                text: text,
                message_side: message_side
            });
            message.draw();
            return $messages.animate({ scrollTop: $messages.prop('scrollHeight') }, 300);
        };
        $('.send_message').click(function (e) {
            return sendMessage(getMessageText());
        });
        $('.message_input').keyup(function (e) {
            if (e.which === 13) {
                return sendMessage(getMessageText());
            }
        });
        sendMessage("metabolism blah blah blah");
        setTimeout(function () {
            return sendMessage("Metabolism is the sum total of all of enzyme-catalyzed reactions in the cells. Exergonic reactions result in energy release whereas endergonic reactions result in stored or absorbed energy. Heat is continually being produced in the body as a product of metabolism. As heat is produced, it is also continuously being lost to the environment. Metabolism could be in the form of carbohydrate, lipid or protein metabolism. Aerobic or oxidative, energy system is highly engaged when adequate oxygen is available. The majority of energy derived from aerobic metabolism comes from the oxidation of CHOs and fats. Energy is defined as the ability to perform work, and this changes in proportion to the magnitude of work done. Basal metabolic rate (BMR) is the minimal level of energy needed to sustain bodily functions. The higher the body mass ( larger the body size), the higher the BMR. Lean body mass (LBM; or fat-free mass) is a strong component of BMR. This is why resistance training(RT) is important to increasing BMR and reducing body fat.  ");
        }, 2000);
    });
}.call(this));
    </script>

下面是有错误的部分代码。 我怎样才能确保文本保留在代码中的“”内:

return sendMessage("Metabolism can be measured using the resting metabolic rate levels. Prediction equations can also be used to give estimates of resting energy expenditure. Using the Harris-Benedict Equation, Resting metabolic rate (RMR) is calculated as kilo calories per day.
For males: RMR = 88. 32 + (4. 799 x height in cm) + (13. 397 x weight in kg) - (5. 677 x age in years).
For females: RMR = 447. 53 + (3. 098 x height in cm) + (9. 247 x weight in kg) - (4. 33 x age in years).
We also use correction factors for physical activity levels as follows:
1) Sedentary: RMR x 1. 4.
2) Moderately active: RMR x 1. 6.
3) Highly active: RMR x 1. 8. In the resting state, one needs to use up about 1. 1 kcal/minute, which requires use of 200 to 250 ml/minute of oxygen. Metabolism can be measured using the resting metabolic rate levels. Prediction equations can also be used to give estimates of resting energy expenditure. Using the Harris-Benedict Equation, Resting metabolic rate (RMR) is calculated as kilocalories per day. In the resting state, one needs to use up about 1. 1 kcal/minute, which requires use of 200 to 250 mL/minute of oxygen. Total energy expenditure or BMR is a total of resting metabolic rate (RMR), comprising of up to 60%75% of the total, the thermogenic effect of physical activity taking up to 15%30%, and thermic effect of food consumption and digestion comprising of 10%. ");
        }, 2000);

最佳答案

简单地使用 ES6 模板字面量,这些字面量是为解决这个问题而设计的(除其他外)。

return sendMessage(`Metabolism can be measured using the resting metabolic rate levels. Prediction equations can also be used to give estimates of resting energy expenditure. Using the Harris-Benedict Equation, Resting metabolic rate (RMR) is calculated as kilo calories per day.
For males: RMR = 88. 32 + (4. 799 x height in cm) + (13. 397 x weight in kg) - (5. 677 x age in years).

For females: RMR = 447. 53 + (3. 098 x height in cm) + (9. 247 x weight in kg) - (4. 33 x age in years).
We also use correction factors for physical activity levels as follows:
1) Sedentary: RMR x 1. 4.
2) Moderately active: RMR x 1. 6.
3) Highly active: RMR x 1. 8. In the resting state, one needs to use up about 1. 1 kcal/minute, which requires use of 200 to 250 ml/minute of oxygen. Metabolism can be measured using the resting metabolic rate levels. Prediction equations can also be used to give estimates of resting energy expenditure. Using the Harris-Benedict Equation, Resting metabolic rate (RMR) is calculated as kilocalories per day. In the resting state, one needs to use up about 1. 1 kcal/minute, which requires use of 200 to 250 mL/minute of oxygen. Total energy expenditure or BMR is a total of resting metabolic rate (RMR), comprising of up to 60%75% of the total, the thermogenic effect of physical activity taking up to 15%30%, and thermic effect of food consumption and digestion comprising of 10%.`);

关于带有文本换行符的 JavaScript 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51799640/

相关文章:

javascript - 根据服务变量更改 CSS

css - 棘手的背景颜色 : Possible with CSS?

javascript - FabricJs - 缩放 Canvas 以适合对象

javascript - 使用Javascript左右摆动图像?

html - 由于在页面顶部插入文本而导致的空白

css - 标题周围的神秘填充

html - 倒 Angular 边框不同容器颜色 CSS

javascript - 在 Internet Explorer 中调试 JavaScript 时浏览器无法交互

javascript - 你能强制IE8识别背景大小: 100%?吗

javascript - 使用阿拉伯语时数字会颠倒