jQuery text() Method

jQuery text() Method

本文关键字:Method text jQuery      更新时间:2023-09-26

我有这个代码:

JavaScript/jQuery:

$('#eform input').each(function() {
    if (this.className.indexOf('required') != -1) {
        $(this).closest('.rowElem').find('label').text(function(_, txt) {
            return '*' + txt
        });
    }
});

我怎样才能返回红色的星号。??

return '<span style="color:red">*</span>' + txt

并使用.html()而不是.text()

您不能返回红色文本,而是将其包装在html中并返回它;这样的事情应该适合您:

$('#eform input').each(function() {
    if (this.className.indexOf('required') !== -1) {
        $(this).closest('.rowElem').find('label').html(function(_, txt) {
            return '<span style="color:red">*</span>' + txt;
        });
    }
});

我在这里使用了 .html() 方法而不是 .text() 方法。