English 中文(简体)
每个索引都使用 Javascript/ ActionScript 吗?
原标题:Javascript/ActionScript do for each indexOf?

代码下方的截图显示我的问题。 它只显示一例文字的彩虹。

我怎样才能得到这个代码 来做每个[脑 指定[/脑 文本?

它实际上用于ActionScript, 但它在 Javascript 中也有效, 所以我一直在测试 < a href=" "http://jsfiddle.net" rel = "no follown noreferrer" >http://jsfidd.net

var txt = "This is a [rainbow]test to show that I can[/rainbow] make whatever I want [rainbow]appear as a rainbow[/rainbow] because I am [rainbow]awesome[/rainbow].";

if ((txt.indexOf("[rainbow]") > -1) && (txt.indexOf("[/rainbow]") > -1)) {
    var firstChar = txt.indexOf("[rainbow]") + 9;
    var lastChar = txt.indexOf("[/rainbow]");

    var RAINBOWTEXT =   ;
    var i = firstChar;

    while (i < lastChar) {
        RAINBOWTEXT += txt.charAt(i);
        i++
    }
    var text = RAINBOWTEXT;
    var texty =   ;

    colors = new Array( ff00ff ,  ff00cc ,  ff0099 ,  ff0066 ,  ff0033 ,  ff0000 ,  ff3300 ,  ff6600 ,  ff9900 ,  ffcc00 ,  ffff00 ,  ccff00 ,  99ff00 ,  66ff00 ,  33ff00 ,  00ff00 ,  00ff33 ,  00ff66 ,  00ff99 ,  00ffcc ,  00ffff ,  00ccff ,  0099ff ,  0066ff ,  0033ff ,  0000ff ,  3300ff ,  6600ff ,  9900ff ,  cc00ff );
    var i = 0;

    while (i <= text.length) {
        var t = text.charAt(i);

        if (t != undefined) {
            texty += "<font color="#" + colors[i % colors.length] + "">" + t + "</font>";
            i++;
        }
    }

    texty = texty.replace("> <", ">&nbsp;<");
    var REPLACEME = "[rainbow]" + RAINBOWTEXT + "[/rainbow]";
    txt = txt.replace(REPLACEME, texty);
    document.write(txt);
}​

""https://i.sstatic.net/rZruv.png" alt="此处输入图像描述"/ >

最佳回答

建立环形 。 < a href=" https:// developmenter.mozilla.org/en/JavaScript/Reference/ Global_Objects/String/indexof" rel="nofollow"\\ code>.indexof 可以将起点作为第二个参数,因此,在 LastChar+10 开始下一次迭代应该有效 。

除此之外,用regex和.replace :

return txt.replace(/[rainbow](.+?)[/rainbow]/g, function(all, match, index, str) {
    return createRGBrainbowArray(match.length).map(function(color, i) {
        return  <span style="color:# +color+ "> +match[i]+ </span> ;
    }).join("");
});
function createRGBrainbowArray(l) {
    // should return an Array of length l with hexadecimal color strings,
    // representing a nice rainbow
}
问题回答

如果我们可以假设没有 [binbow] 标签的中间或嵌套,我只需使用一个简单的>>replerback :

var rainbowified = txt.replace(/[rainbow](.*?)[/rainbow]/, function(textWithTags, textBetweenTheseTags) {
    var text = textBetweenTheseTags;
    ....
    for(var i = 0; i < text.length; ++i) {
        // rainbowify each letter of text...
    }
    ...
    return textWithFontTags;
}

您可以使用此选项获得新字符串, 包含您想要的转换 。

此外, font 标签已被取消定价;您应该在 span 属性中使用 span ,并带有 color:#XXXXXX 属性中 样式

var colors = [
   f0f ,  f0c ,  f09 ,  f06 ,  f03 ,  f00 ,  f30 ,  f60 ,  f90 ,  fc0 ,
   ff0 ,  cf0 ,  9f0 ,  6f0 ,  3f0 ,  0f0 ,  0f3 ,  0f6 ,  0f9 ,  0fc , 
   0ff ,  0cf ,  09f ,  06f ,  03f ,  00f ,  30f ,  60f ,  90f ,  c0f 
];
function rainbowify(text) {
  return text.replace(/[rainbow](.*)[/rainbow]/g, function(_, inner){
    return inner.replace(/./g, function(ch, i){
      return  <span style="color:#  + colors[i % colors.length] +  ;">  + ch +  </span> ;
    });
  })
}

我该怎么做呢?





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签