所以基本上我只是想简化这个:
$('iframe[src*="foo"],iframe[src*="bar"]');
类似于:
var sources = ['foo', 'bar','something'];
$('iframe[src*="some string in array"]');
希望你明白这一点:)
答案 0 :(得分:2)
您可以将变量插入选择器,如下所示:
var sources = ['foo', 'bar','something'];
$('iframe[src*="' + sources[0] + '"]');
请参阅下面的图片示例。具有红色边框的那些被选为数组的一部分
var arr = ["http://placebear", "http://lorempixel", "http://placehold", "http://loremflickr"];
for(var i = 0; i < arr.length; i++) {
var $img = $('img[src*="'+arr[i]+'"]');
$img.addClass('inarray');
console.log($img);
}
img {
border: 3px solid #000;
border-radius: 50%;
}
img.inarray {
border-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="http://placebear.com/200/200" />
<img src="http://placecage.com/200/200" />
<img src="http://lorempixel.com/200/200" />
<img src="http://placehold.it/200x200" />
<img src="http://www.fillmurray.com/200/200" />
<img src="http://loremflickr.com/200/200" />
答案 1 :(得分:0)
var sources = [&#39; foo&#39;,&#39; bar&#39;,&#39; something&#39;];
$(&#39; iframe中[SRC * =&#34;&#39; +源[Math.floor(的Math.random()* sources.length)] +&#39;&#34;] &#39);