我想創建兩個按鈕:一個向網頁添加一個圓,一個刪除一個圓。我想創建兩個按鈕:一個向網頁添加一個圓,一個刪除一個圓
舞臺上不能超過5個圓圈。如果單擊添加按鈕並且頁面上有五個圓圈,將會彈出一個警告,告訴用戶不能再添加圓圈。
var circle = document.getElementById('#div');
$(function() {
$('#buttonOne').on('click', addItem);
$('#buttonTwo').on('click', removeItem);
});
function addItem(){
if (circle > 5) {
alert('You cannot add more than 5 objects');
} else {
document.body.appendChild(div);
};
}
function removeItem(){
if (circle== 0) {
alert('You have not added anything yet');
} else {
$(this).remove();
};
}
.circle {
display: block;
width: 50px;
height: 50px;
border-radius: 50%;
transition: background-color 350ms;
background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="Add Circle" id="buttonOne"/>
<input type="button" value="Delete Circle" id="buttonTwo"/>
<div class="circle"></div>
<p></p>
<script src="week4.js" type="text/javascript"></script>
哇謝謝!我正在尋找確切的反饋。 –