如何使用jquery更改大写和小写字符的大小写

如何使用jquery更改大写和小写字符的大小写,jquery,Jquery,有没有办法产生这样的产出,第一个字母是资本,第二个字母是小字母,第三个字母是资本,第四个字母是小字母,依此类推: 输入#输入代码:This stackoverflow.com 输出#您已输入:ThIs StAcKoVeRfLoW.CoM 以下是我到目前为止尝试过的片段 $(“#code”).keyup(函数(){ var finalCode=$(“#代码”).val(); 对于($i=0;$i $(“#code”).keyup(函数(){ var finalCode=$(“#代码”).val(

有没有办法产生这样的产出,第一个字母是资本,第二个字母是小字母,第三个字母是资本,第四个字母是小字母,依此类推:

输入#输入代码:This stackoverflow.com

输出#您已输入:ThIs StAcKoVeRfLoW.CoM

以下是我到目前为止尝试过的片段

$(“#code”).keyup(函数(){
var finalCode=$(“#代码”).val();
对于($i=0;$i
$(“#code”).keyup(函数(){
var finalCode=$(“#代码”).val();
对于($i=0;$i
$(“#code”).keyup(函数(){
var finalCode=$(“#代码”).val();
对于($i=0;$i请使用方法并执行类似的操作

$(“#code”).on('input',function(){
var i=0;
$(“#outcode”).html(
//匹配所有非空格字符并将其替换为
//i的值是奇数或偶数
此.value.replace(/\S/ig,函数(m){
//校验值为偶数,i的增量值为
如果(i++%2==0)
//转换成大写
m=m.toUpperCase();
//返回replca文本
返回m;
})
);
});

输入代码:

您已进入:
我建议使用简单的字符串和数组方法,而不是使用正则表达式:

function alternateCapitals() {
  // retrieving the <input> element's value:
  var text = this.value
             // removing the leading, and trailing, white-space:
             .trim()
             // splitting the String into an Array, with each
             // array-element being a single-character:
             .split('')
             // using Array.prototype.map() to return a
             // modified version of the original characters:
             .map(function(char, index) {
               // char: the first argument, is the current
               // array-element of the array over which
               // we're iterating;
               // index: the second arguement, is the index
               // of the current array-element in the array
               // over which we're iterating.

    // if the remainder of the index divided by 2 is zero we
    // return the character in upper-case, otherwise we return
    // the character in lowercase:
    return index % 2 === 0 ? char.toUpperCase() : char.toLowerCase();

  // we join the Array of characters back together into a string,
  // using Array.prototype.join():
  }).join('');

  // setting the text of the '#outcode' element:
  $('#outcode').text(text);
}

$("#code").keyup(alternateCapitals);
标签{
显示:块;
}

输入代码:
您已输入:
您也可以使用$('#outcode').css('text-transform','capitale');我不想使用“这是我的测试”这种类型的输出…第一…第三…第五…第七…字符id大写。。。