gpt4 book ai didi

javascript - 使 jQuery 完全不区分大小写

转载 作者:行者123 更新时间:2023-12-03 12:31:18 25 4
gpt4 key购买 nike

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

例如,结果表的类有时是“Grid”,有时是“grid”。

所以我需要在测试代码中使用不同的 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')

参见demo

关于javascript - 使 jQuery 完全不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23907546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com