我的页面上有一些单词显示“存在”或“缺席”,用户可以单击它们在存在或缺席之间切换。
当该词设置为“缺席”时,我希望该文本为红色。
该单词代表 bool 值,并使用以下代码在屏幕上更新:
<script type="text/javascript">
absentText = $.trim('@MyApp.Resources.MyAppResource.Absent_Text').toLowerCase();
presentText = $.trim('@MyApp.Resources.MyAppResource.Present_Text').toLowerCase();
updateAttendanceUrl = '@Url.Action("UpdateAttendance", "Attendance")';
</script>
如果 MyAppResource.Absent_Text = 缺席,页面会正常显示“缺席”一词。
当我将 MyAppResource.Absent_Text 更改为“缺席”时,我的页面字面显示 <span style="color:red">absent</span>
这是我的 View 源代码的示例:
<td><span style="color:red">absent</span></td>
不知何故,我的 < 和 > 符号被拿走了。
如何更改代码,以便当屏幕上的单词写为“缺席”时,它会显示为红色? 有没有更简单的方法来将屏幕上与“不存在”匹配的任何文本着色为红色?
作为引用,这里是我页面中的其余 javascript:
var userId;
var attendanceDay;
var isPresent;
var updateAttendanceUrl;
var absentText;
var presentText;
var courseId;
$(document).ready(function (event) {
$('.attendance').live('click', function () {
userId = $(this).parents('tr').attr('id');
if (userId != '') {
attendanceDay = $(this).attr('id');
isPresent = $.trim($(this).find('span').text().toLowerCase());
courseId = $(this).parents('tbody').attr('id');
$(this).find('span').addClass('currentClass');
if (isPresent == absentText) {
UpdateAttendance(1);
} else {
UpdateAttendance(0);
}
} else {
event.preventDefault();
}
});
});
function UpdateAttendance(present) {
url = updateAttendanceUrl;
$.ajax({
type: "POST",
url: url,
data: "userId=" + userId + "&attendanceDay=" + attendanceDay + "&courseId=" + courseId + "&present=" + present,
success: function (data) {
if (isPresent == absentText) {
$('#' + userId).find('.currentClass').text(presentText).removeAttr('class');
} else {
$('#' + userId).find('.currentClass').text(absentText).removeAttr('class');
}
return true;
}
});
}
最佳答案
您要查找的内容称为 unescape 或 htmldecode。 SO 上已经有各种可用的主题。这是one .
关于javascript - 当我想在 span 标签内渲染文本时看到 span 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12178387/