简体   繁体   English

从数组中获取一部分属性值

[英]Get a part of attribute value from array

So basically i'm just trying to simplify this: 所以基本上我只是想简化一下:

$('iframe[src*="foo"],iframe[src*="bar"]');

to something like: 像这样:

var sources = ['foo', 'bar','something'];

$('iframe[src*="some string in array"]');

Hope you get the idea :) 希望你有这个主意:)

You can insert the variable into the selector, like this: 您可以将变量插入选择器,如下所示:

var sources = ['foo', 'bar','something'];
$('iframe[src*="' + sources[0] + '"]');

See below for an example with images. 请参阅下面的图像示例。 The ones with red borders are selected as part of the array 带红色边框的那些被选择为数组的一部分

 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" /> 

var sources = ['foo', 'bar','something']; var sources = ['foo','bar','something'];

$('iframe[src*="'+sources[Math.floor(Math.random()*sources.length)]+'"]'); $( 'IFRAME [SRC * = “ '+源[Math.floor(的Math.random()* sources.length)] +”']');

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

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