有jQuery字符串比较问题

时间:2010-10-21 01:43:05

标签: javascript jquery string jquery-selectors

我有一个小提琴here显示我正在尝试做什么。

我有一个动态生成的表,因此列可以按用户选择的顺序显示。所以,我正在尝试获取两个特定标头的索引,以便我可以为这两个列添加一个CSS类供以后使用。

1 个答案:

答案 0 :(得分:2)

你应该在这里使用.filter()(并且只要你需要限制元素集),因为你的.each() return被扔掉了,就像这样:

//Loop thru the headers and get the Supp elem
var suppCol = $("#my_table th").filter(function() {
    return $(this).html() == "Supp";
});
//Loop thru the headers and get the Report elem
var reportCol = $("#my_table th").filter(function() {
    return $(this).html() == "Report";
});

You can test the updated/working fiddle here。使用.each()的替代方案看起来像是:

var suppCol, reportCol;
$("#my_table th").each(function() {
    var $this = $(this), html = $this.html();
    if(html == "Supp") suppCol = $this;
    if(html == "Report") reportCol= $this;
});

You can test that version here

相关问题