2011-12-21 56 views
1

可能重复:
How to select first row of the first table in an html page using jQuery?如何在html页面中选择第一个表格的第一行,并使用jQuery将其突出显示?

想,如果我有我的HTML页面的多个表(没有他们的“id”属性),所以我怎么能选择第一个表格的第一行或使用jQuery选择器的任何特定表?之后我可以使用任何颜色突出显示它。实际上,我想通过后面的代码来完成它。我曾尝试过,但我运气不好。我的代码是:

ScriptManager.RegisterStartupScript(
    this, 
    this.GetType(), 
    "alert", 
    "<script language='javascript'>alert('hi'); 
     $('table:tableOne').children('tr:first').css('background-color', '#ffdc87'); 
     alert('end');</script>", 
    false); 

因为它显示有任何语法错误。

+0

它不重复它的类似,但我认为我的要求是比这更多。 – 2011-12-21 07:36:46

+0

加入'.css()'是不是真的是一个挑战呢? – fyr 2011-12-21 07:37:34

+0

不,但在我的情况下,它不工作..所以我在这里问,否则,我不会先生。 – 2011-12-21 07:39:15

回答

2

这将定义一个类“亮点”有背景的黄色,然后选择第一个的第一表排,并给它一个“突出”类。

<style type="text/css"> 
    tr.highlight td { background-color:#ff0; } 
</style> 
<script type="text/javascript"> 
    $('table:eq(0) > tr:eq(0)').addClass('highlight'); 
</script> 
+1

用于从jQuery中分离CSS值的+1,使用'.css()'是icky。 – Interrobang 2011-12-21 07:48:58

+1

@Interrobang他分离它,因为这是正确做到这一点的唯一方法 – fyr 2011-12-21 07:52:14

+1

@fyr:我相信你已经在这里足够长的时间来看到直接使用JavaScript添加CSS属性的trainwreck答案:P – Interrobang 2011-12-21 07:53:45

0

的第一个表的第一行:

$('table:first').children('tr:first').css('background-color', '#ffdc87'); 

任何表的第一行:

$('table').each(function(){ 
$(this).children('tr:first').css('background-color', '#ffdc87'); 
}); 
相关问题