請檢查下面的代碼。我想在點擊'開關'按鈕後改變一個css屬性。我在這裏做什麼錯了?如果可能,請幫助我以任何其他方式做到這一點。感謝所有使用jquery更改css屬性
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<button id="myButton">switch</button>
<p class="foo">Some text</p>
<script type="text/javascript">
$("#myButton").click(function() {
$(".foo").attr("background", "red");
});
</script>
<style>
.foo {
background: gray;
}
</style>
</body>
</html>