更正jQuery以替换<span>元素中的内容</span>

时间:2011-12-11 16:20:24

标签: javascript jquery html

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

假设我有machType

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

3 个答案:

答案 0 :(得分:1)

$(document).ready( 
    $(".machType").each(function(index, value) {
        $(value).html($(value).html().replace('machine1 (windows)', 'Windows Machine 1'));
    }));

答案 1 :(得分:1)

这样的事情应该有所帮助(给每个span class machType}:

$(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("");
        }));
    });
});

答案 2 :(得分:0)

$(document).ready( $("#machType").each(function() {
    $(this).html($(this).html().replace('machine1 (windows)', 'Windows Machine 1');
}));