简体   繁体   English

javascript / jquery有条件地用数组中的随机文本替换文本

[英]javascript/jquery to replace text conditionally with random text from an array

I would like to replace a string with a random string from an array if that string equals a certain condition. 如果该字符串等于某个条件,我想用数组中的随机字符串替换该字符串。

So far I have this (which doesn't address the condition part). 到目前为止,我有这个(它没有解决条件部分)。

the html: HTML:

<div>
   <span class ="test">foo</span>
</div>
<div>
   <span class ="test">bar</span>
</div>
<div>
   <span class ="test">test</span>
</div>
<div>
   <span class ="test">random</span>
</div>

the code: 编码:

$(".test").each(function () {
var quotes = new Array("foo", "bar", "baz", "chuck"),
    randno = quotes[Math.floor(Math.random() * quotes.length)];

    $('.test').text(randno);
});

This sets every ".test" class the same thing. 这会将每个“ .test”类都设置为相同。 I get: 我得到:

foo FOO
foo FOO
foo FOO
foo FOO

or 要么

bar 酒吧
bar 酒吧
bar 酒吧
bar 酒吧


  1. How do I make this only replace the string if it equals say "foo"? 如何使它仅替换等于“ foo”的字符串?

  2. If I have multiple "foos" How do i get each "foo" it replaces to be random not all set to the same thing? 如果我有多个“ foos”,我如何获得每个“ foos”,以随机方式替换,而不是全部都设置为同一东西?

You need to use this in the .each() callback method 您需要使用this.each()回调方法

$(".test").each(function() {
    var quotes = new Array("foo", "bar", "baz", "chuck"),
        randno = quotes[Math.floor(Math.random() * quotes.length)];

    //Check condition
    if ($(this).text() === "foo") {
        $(this).text(randno);
    }
});

Alternatively you can also use .text(function) 或者,您也可以使用.text(function)

var quotes = new Array("foo", "bar", "baz", "chuck");
$(".test").text(function(_, text) {
    var randno = quotes[Math.floor(Math.random() * quotes.length)];

    //Check condition
    if (text === "foo") {
        return randno;
    }
    return text;
});

 $(function() { var quotes = new Array("foo", "bar", "baz", "chuck"); $(".test").text(function(_, text) { var randno = quotes[Math.floor(Math.random() * quotes.length)]; //Check condition if (text === "foo") { return randno; } return text; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <span class="test">foo</span> </div> <div> <span class="test">bar</span> </div> <div> <span class="test">test</span> </div> <div> <span class="test">random</span> </div> 

Another approach is to shuffle the replacements array, than use it: 另一种方法是改组替换数组,而不是使用它:

 /* Famous shuffle function */ Array.prototype.shuffle = function() { for (var j, x, i = this.length; i; j = Math.floor(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x); return this; }; $.fn.extend({ randomReplace: function(text, replacements) { replacements.shuffle(); return this.each(function () { if( $(this).text().toLowerCase()==text.toLowerCase() ) $(this).text(replacements.pop()); }); } }); $('.test').randomReplace('foo', ['one', 'two', 'three', 'four', 'five', 'six']); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <span class="test">foo</span> <span class="test">bar</span> <span class="test">foo</span> <span class="test">foo</span> <span class="test">bar</span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM