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



在控制台上运行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%.`);

您需要对用单引号或双引号定义的字符串中的新行进行转义。在每一行的末尾,放置一个反斜杠,当您要将其呈现为 HTML 时,请在前面放置一个<br>标记:

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.<br>
For males: RMR = 88. 32 + (4. 799 x height in cm) + (13. 397 x weight in kg) - (5. 677 x age in years).<br>
For females: RMR = 447. 53 + (3. 098 x height in cm) + (9. 247 x weight in kg) - (4. 33 x age in years).<br>
We also use correction factors for physical activity levels as follows:<br>
1) Sedentary: RMR x 1. 4.<br>
2) Moderately active: RMR x 1. 6.<br>
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);

您需要连接字符串。

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."
+ "nFor males: RMR = 88. 32 + (4. 799 x height in cm) + (13. 397 x weight in kg) - (5. 677 x age in years)."
+ "nFor females: RMR = 447. 53 + (3. 098 x height in cm) + (9. 247 x weight in kg) - (4. 33 x age in years)."
+ "nWe also use correction factors for physical activity levels as follows:");

最新更新