ver*_*ric 5 jquery attributes case-insensitive selector
我正在尝试为YouTube编写用户脚本,该用户脚本将根据其标题选择页面上的所有视频,这些视频也会设置为缩略图图像的title属性.
以下工作正常,但它区分大小写:
var videoTitle = "the";
$('img[title*="'+videoTitle+'"]').each(function() {
// stuff here
});
Run Code Online (Sandbox Code Playgroud)
上面的选择器将匹配标题中带有"the"的任何视频,但不匹配"The"或"THE".
我确实读过我可以用来.filter()以某种方式完成这个,但我不确定它是如何工作的,我找不到一个能够成功适应我的场景的例子.
我尝试了类似这样的东西,基于我在StackOverflow上找到的一个例子,但它不起作用:
$('img[title]').filter(function(){return this.title.toLowerCase() == videoTitle}).each(function() {
// stuff here
});
Run Code Online (Sandbox Code Playgroud)
这是根据Karl Swedburg在"属性包含选择器" 的jQuery 文档页面上的帖子改编而来的.它使用RegEx和i不区分大小写的开关以及filter功能 -
<img title="Star Wars"/>
<img title="Star Wars 2"/>
<img title="Star Wars 3"/>
<img title="Back To The Future"/>
var videoTitle = "star wars";
var re = RegExp(videoTitle ,"i");
$('img[title]').filter(function() {
return re.test(this.title);
}).each(function() {alert(this.title)});
Run Code Online (Sandbox Code Playgroud)
应该提醒三部"星球大战"电影的名字.
演示 - http://jsfiddle.net/FKBTx/3
| 归档时间: |
|
| 查看次数: |
6125 次 |
| 最近记录: |