$(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)
最佳答案
这样的东西应该会有所帮助(给每个span
一个machType
的class
):
$(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("");
}));
});
});
关于javascript - 正确的 jQuery 来替换 <span> 元素中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8465469/