如何在逗号分隔符后交换文本?

时间:2014-05-15 16:06:07

标签: javascript jquery

目前正在生成以下代码并生成'LastName, FirstName'

<div id="welcomeMenuBox">
 <spanid="zz4_Menu_t" class="ms-menu-althov ms-welcome-root">
   <a id="zz4_Menu" class="ms-core-menu-root" title="Open Menu" href="javascript:;">LastName, FirstName</a>
 </span>
</div>

我想在页面加载时交换文本,以便在JQuery或JavaScript中显示Welcome FirstName, LastName

3 个答案:

答案 0 :(得分:3)

您可以使用:

var text = $('#zz4_Menu').text().split(',');
$('#zz4_Menu').text('Welcome ' + text[1] + ', ' + text[0]);

<强> Fiddle Demo

答案 1 :(得分:2)

JavaScript

var a = document.getElementById('zz4_Menu')
var res = a.innerHTML.split(",")
a.innerHTML = "Welcome " + res[1] + "," + res[0]

Example

答案 2 :(得分:2)

这是一种方法 -

var currentText = $('.ms-core-menu-root').text();
var arrayText = currentText.split(',');
var newText = 'Welcome ' + arrayText[1] + ' ' + arrayText[0];
$('.ms-core-menu-root').text(newText);