2013-08-12 64 views
0

我有一个ID和一个类的表。我正在使用一个插件修复前两栏和标题以满足我的要求。我的表格由标签定义:如何在JavaScript或jQuery中将属性'id'添加到类中?

<table class="fancyTable" id="myTable03" cellpadding="0" cellspacing="0"> 

我想在此表中添加其他功能。通过萤火虫检查DOM后,我发现桌子被插件分解成两部分(我认为)。虽然表中的一部分成为

<table class="fht-table fancyTable" style="margin-top: 0px;"> 

,另一个成为

<table id="myTable03" class="fancyTable fht-table fht-table-init" cellspacing="0" cellpadding="0" style="width: 926px; margin-top: -40px;"> 

我想属性ID添加到包含类“FHT-表fancyTable”表。我怎样才能做到这一点?我使用的是jquery/javascript。

+0

请发表你有什么到目前为止已经试过。 jQuery文档有一个关于DOM操作的部分,你可能想先看看它:http://api.jquery.com/category/manipulation/general-attributes/。如果你不知道如何选择正确的元素,请查看[jQuery multiple class selector](http://stackoverflow.com/q/1041344/218196)。 –

回答

1

你已经有一个ID第二个myTable03

如果你想添加attribute id表包含类fht-table fancyTable

那就试试这个,

$(".fht-table.fancyTable:not('#myTable03')").attr('id','myTable04'); 

How can I select an element with multiple classes?

+0

你绝对给我看这个提示。 $(“.fht-table.fancyTable”)。attr(“id”,“myTable”); \t \t \t $(“。fht-table.fancyTable.fht-table-init”)。attr(“id”,“myTable03”);我现在得到了我的答案。 – L4reds

2

您需要s omething这样的:

$('.fht-table.fancyTable').not('#myTable03').prop('id', 'yourId'); 

$('.fht-table.fancyTable').not('.fht-table-init').prop('id', 'yourId'); 
0

属性ID可以被添加作为用于属性正常归属。但是对该元素的引用不应该由Id获得。

您可以使用:

$("div").attr("id", "div1"); 

注:两个元素不能有相同的ID。

var divs = document.getElementsByTagName('div'); 

var divs = document.getElementsByClassName('fancyTable'); 


for(var i = 0; i<divs.length;i++) { 
    divs[i].setAttribute("id","div"+i); 
} 
相关问题