正确的jQuery替换<span>元素

Correct jQuery for replacing content within a <span> element

本文关键字:span 元素 jQuery 替换      更新时间:2023-09-26

$(document).ready( $("#machType").each(function() {$(this).replace('machine1 (windows)', 'Windows Machine 1');})); ";上面的代码片段对于更改span的内容是否正确?(当然是在标签内)

假设我有machType

<span id="machType">Machine 1 (windows)</span></span id="machType">Machine 2 (windows)

这样做应该会有所帮助(给每个span一个classmachType):

$(document).ready(function () {
    $(".machType").each(function () {
        $(this).text($(this).text().replace(/Machine ('d+) '((.*)')/, function (full, num, os) {
            return [os[0].toUpperCase(), os.substring(1), " Machine ", num].join("");
        }));
    });
});
$(document).ready( 
    $(".machType").each(function(index, value) {
        $(value).html($(value).html().replace('machine1 (windows)', 'Windows Machine 1'));
    }));
$(document).ready( $("#machType").each(function() {
    $(this).html($(this).html().replace('machine1 (windows)', 'Windows Machine 1');
}));