我想在 html() 中设置文本样式,例如 color:red;字体粗细:粗体
。我怎样才能在 jQuery 本身中做到这一点?我可以在 html 中使用 id=login_result
设置 div 的样式,但这里的每个文本都是红色和粗体。我只希望像下面这样的这些错误文本只显示红色。非常感谢
var login_result = $('#login_result');
if ($.trim(email.val()).length == 0) {
email.focus(); // focus to the filed
login_result.html('<span> Email field is required</span>');
return false;
}
最佳答案
您可以向跨度添加一个类并在您的样式表中定义它:
var login_result = $('#login_result');
if ($.trim(email.val()).length == 0) {
email.focus(); // focus to the filed
login_result.html('<span class="error"> Email field is required</span>');
return false;
}
.error {
color: red;
}
与 Brad Martin 的解决方案相比,这具有优势,所有关于样式的信息都在您的样式表中设置,而不是在主要处理应用程序逻辑的 javascript 中设置。
关于javascript - 如何在 jquery 的 .html() 中设置文本样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28284348/