开发者

Having jQuery string comparison issues

I've got a fiddle going here to show what I'm trying to do.

I have a table that is generated dynamically, so the columns could appear in whatever order the user chooses. So, I'm trying to get the index of two specific headers so that I can a开发者_如何学Pythondd a CSS class to those two columns for use later.


You should use .filter() here instead (and whenever you need to restrict the element set), since your .each() return is getting thrown away, like this:

//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. The alternative using .each() would look like tis:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜