繁体   English   中英

使jQuery完全不区分大小写

[英]Making jQuery completely case-insensitive

我正在使用jQuery进行自动化测试。 应用程序不同部分的HTML看起来非常相似,但是有时类名,id和样式的大写字母是不同的。

例如,结果表的类是某些“网格”,有时是“网格”。

因此,我需要在测试代码中使用不同的jQuery表达式(在Java中):

public String getResultCell(int row, int colum) throws Exception {
  return _browser.evaluateJavaScript("$('table.Grid').find('tbody').find('tr').eq(" + row + ").find('td').eq(" + colum + ").text()").trim();
}

有时

// FIXME: fix case sensitivity
public String getResultCell(int row, int colum) throws Exception {
  return _browser.evaluateJavaScript("$('table.grid').find('tbody').find('tr').eq(" + row + ").find('td').eq(" + colum + ").text()").trim();
}

有没有一种方法可以使jQuery完全不区分大小写?

此jQuery代码应按类名称查找表,而不取决于大小写

var classname = 'grid';
$('table').filter(function(){
   return (" "+$(this).get(0).className.toLowerCase()+" ".indexOf(' '+classname.toLowerCase()+' ')!=-1);
});

您可以将其包装为jquery方法:

$.fn.filterByClass = function (classname) {
    return $(this).filter(function(){
       return (" "+$(this).get(0).className.toLowerCase()+" ".indexOf(' '+classname.toLowerCase()+' ')!=-1);
    });
};

然后像这样使用: $('table').filterByClass('grid')

观看演示

暂无
暂无

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

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