2016-09-07 32 views
0

您好我想從一個div一個共同的類名中刪除所有ID:查找類名,然後刪除編號

$('div.panel-group').removeAttr('id', ''); 

但不起作用。我不想(如發現ID然後刪除ID),因爲的堆棧生成的ID的

THX我

+1

嘗試只用1個參數:'.removeAttr( 'ID')'| https://api.jquery.com/removeAttr/ – kosmos

+3

'$('div.panel-group')。removeAttr('id');'如果你有2個參數你只是設置 – guradio

+0

不幸的是,這是行不通的。我可以成功做$('#myID123')。removeAttr('id');但我想搜索課程名稱。 – roshambo

回答

0

這一個工作,我與元素的ID顯示一個警告,我刪除的ID該元素,那麼如果我再次顯示警報,則ID未定義。

<div class="panel-group" id="test"> 
First Div 
</div> 
<div class="panel-group" id="test2"> 
Second Div 
</div> 

腳本:

$('div.panel-group').each(function(){ 
      alert($(this).attr('id')); 
     $(this).removeAttr('id'); 
     alert($(this).attr('id')); 
}); 

同時檢查的jsfiddle:https://jsfiddle.net/f72q9wao/

不管怎麼說,儘量分享一些HTML,所以我們可以有一個更好的主意。

+0

Thx,但不起作用 – roshambo

0

試試這個。這將解釋你想要什麼。 嘗試點擊第一個div,如果類名等於你的發現名稱,它將刪除該div的id,然後它將刪除所有樣式,對於該id.try它。希望這會對你有所幫助。

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title></title> 
 
\t <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
\t 
 
</head> 
 

 
<style type="text/css"> 
 
div.container div{ 
 
\t width: 100px; 
 
\t height: 100px; 
 
\t border:1px solid black; 
 
\t margin:20px; 
 
\t cursor: pointer; 
 

 
} 
 

 
.first{ 
 
\t background-color: orange; 
 
} 
 

 
.second{ 
 
\t background-color: pink; 
 
} 
 

 
.third{ 
 
\t background-color: purple; 
 
} 
 

 
#firstid{ 
 
\t border:10px solid red; 
 
} 
 

 
</style> 
 

 
<body> 
 
\t 
 
<div class="container"> 
 
\t <div class="first" id="firstid">class = "first" and <br> id="firstid"</div> 
 
\t <div class="first" id="secondid">class = "first" and <br>id="secondid"</div> 
 
\t <div class="second" id="thirdid">class = "second" and <br>id="thirdid"</div> 
 
\t <div class="third" id="fourthid">class = "third" and <br>id="fourthid"</div> 
 
</div> 
 

 

 
</body> 
 

 
<script type="text/javascript"> 
 
$(document).ready(function() 
 
{ 
 
    \t $("div.container div").click(function(){ 
 
    \t \t var the_class = $(this).attr('class');//get the class name of clicked one 
 
    \t \t var the_id = $(this).attr('id');//get the id of the clicked 
 

 
    \t \t alert("The class name :" +the_class + " and the id :"+the_id); 
 

 
    \t \t //then implment your condition 
 
    \t \t if (the_class == "first") 
 
    \t \t { 
 
    \t \t \t $("[class='first']").removeAttr('id'); 
 
    \t \t } 
 

 

 

 
    \t }); 
 
    \t 
 
}); 
 

 

 
</script> 
 
</html>