2014-02-23 86 views
0

我試圖通過將-img添加到類的末尾來更改類到icon-local-img的類。jquery更改div問題的類

這是我正在嘗試。爲什麼它不起作用?

HTML

<div class="icon-local">sdvdfvsdfvdfs fdsfggfd</div> 

CSS

.icon-local { 
    color:#00f; 
} 
.icon-local-img { 
    color:#f00; 
} 

JS

$(document).ready(function() { 
    function swapClass($obj) { 
     $($obj).toggleClass($obj + ' ' + $obj + '-img'); 
    } 
    if (!$('html').hasClass('fontface')) { 
     swapClass('icon-local'); 
     swapClass('icon-mail'); 
     swapClass('icon-phone'); 
    } 
}); 

http://jsfiddle.net/7Ka5g/3/

回答

2

Fiddle Demo

function swapClass($obj) { 
    $('.' + $obj).toggleClass($obj + ' ' + $obj + '-img'); 
    //^ class selector 
} 

Class Selector (「.class」)

+1

做;)謝謝! (多麼愚蠢的錯誤) – gespinha

+0

@GEspinha歡迎很高興幫助:) –