javascript - 带有文本换行符的 JavaScript 语法错误
问题描述
在控制台上运行 html 检查时,当拾取一些包含新行的文本时会出现错误。我已经尝试用 纠正它CSS
,white-space: pre-line
但错误仍然存在。以下是没有错误的完整代码:
<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%.`);
推荐阅读
- listview - Xamarin 表单:不同的项目正在调用而不是 flowlistview 中的选定项目?
- c# - 如何使用预构建事件命令行将参数传递给 T4 模板
- angular - 如何将 [] 中的值以角度传递给 API?
- android - 在 Android Studio 中同步项目时出现 Gradle 版本错误
- python - Pydantic 同名模型列表
- python - 在 python 模块中定义一个函数,使函数名等于一个字符串变量
- ruby-on-rails - 类方法规范 - 保留顺序并返回 ActiveRecord 关系
- google-cloud-platform - Google Datastore(Datasore 模式下的 Firebase)是否有限制新帐户的写入吞吐量?
- excel - 如何通过从闭源工作簿中指向表数组在 VBA 中使用 vlookup 函数?
- python - 如何将列表中的 3 个字母添加到 3 个输入字母