使用jquery自动将类更改为id

使用jquery自动将类更改为id,jquery,Jquery,使用jquery可以自动将表中的类更改为id 作为这个引导类的示例 <table class="table table-striped table-bordered"> 到css id <table id="mytable"> 我想使用打印页面的id您只需使用类选择器选择该元素并添加一个id属性,如: $('.table.table-striped.table-bordered').attr('id','mytable'); 但是如果你还想删除这些类 $('

使用jquery可以自动将表中的类更改为id

作为这个引导类的示例

<table class="table table-striped table-bordered">

到css id

<table id="mytable">


我想使用打印页面的id

您只需使用类选择器选择该元素并添加一个
id
属性,如:

$('.table.table-striped.table-bordered').attr('id','mytable');
但是如果你还想删除这些类

$('.table.table-striped.table-bordered').attr('id','mytable').removeAttr('class');
removeAttr('class')
将从
class
属性中删除所有类,并使元素看起来像
中的

,您可以使用:

<table class="table table-striped table-bordered mytable"></table>
$("table.mytable").removeClass("mytable").attr("id","mytable");

$(“table.mytable”).removeClass(“mytable”).attr(“id”,“mytable”);


$(“table.table”).attr(“id”,“mytable”);
第一个方法将
转换为
id

第二种方法只是在所选表中添加一个Id。

是的,但是应该使用什么逻辑,它应该查找特定的类并将其转换为Id吗?或者?如上所述,您需要定义规则。为什么是“我的桌子”而不是别的东西?在所有情况下都应该删除引导表类吗?如果您有两个表(它们不能都有相同的
id
),该怎么办。
<table class="table table-striped table-bordered"></table>
$("table.table").attr("id","mytable");