2014-05-14 158 views
0

當我點擊其中一個按鈕時,我希望段落根據我點擊的按鈕更改大小。這似乎並不奏效。我檢查了一切,並與我的jQuery知識水平(初學者)我無法弄清楚,所以我需要你的幫助。下面是代碼:當點擊按鈕時jQuery添加類

<!DOCTYPE html> 
<html lang="en"> 

<head> 
<meta charset="utf-8"> 

<link rel="stylesheet" type="text/css" href="main.css"> 
</head> 
<body> 
    <input type="button" id="smaller" value="smaller text" /> 
    <input type="button" id="bigger" value="bigger text" /> 
    <p > Some text inside of it.</p> 
    <p > Some text inside of it too!</p> 


    <script type="text/javascript" src="jquery.js"></script> 
    <script type="text/javascript" src="query.js"> </script> 
</body> 

</html> 

jQuery的

$('#bigger').click(function() { 
    $('p').addClass('.bigger'); 
}); 

$('#smaller').click(function() { 
    $('p').addClass('.smaller'); 
}); 

CSS

.bigger { 
    font-size:30px; 
    background-color:red; 

} 
.smaller { 
    font-size:10px; 
} 

回答

4

addClass()刪除.因爲函數已經認識到像一類的字符串,不specifiy類選擇。

試試這個:

$('#bigger').click(function() { 
    $('p').addClass('bigger'); //instead of .bigger 
}); 

$('#smaller').click(function() { 
    $('p').addClass('smaller'); //instead of .smaller 
}); 

DEMO

+0

+1尼斯趕上... – War10ck

+0

您應該解釋WH這是答案。例如:**。addClass()**接受一個或多個空格分隔的類的字符串參數。它不接受選擇器。 – pmandell

+0

太好了。 Tnx很多! – user3638147