你好我對編碼有點新,所以如果我不理解你使用的一些術語,那麼請耐心等待。如何刪除特定的按鈕?
所以基本上我有2個按鈕,其功能是添加和刪除按鈕。一旦添加了按鈕,按鈕的功能就是顯示用戶的位置。
當前按鈕被最後一個按鈕刪除。我如何選擇刪除特定的按鈕?
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<style>
button{
height:100px;
width: 80px;
text-align: center;
margin: 5px 4px;
}
#mainButtons button{
display: inline-block;
margin: auto;
}
</style>
<div id="mainButtons">
<button onclick="addButton()">Add Contact</button>
<button onclick="removeButton()">Remove Contact</button>
</div>
</br>
<div id="que"></div>
<script>
var increment = 0;
function addButton(){
var id = 'btn' + (increment++);
var que = $("#que");
var el = $("<button id='"+id+"'>Click for location</button>");
que.append(el);
clickHandler(el);
}
function removeButton(){
if (increment >= 0) {
$("#btn"+(increment-1)).remove();
increment--;
}
}
function clickHandler(el) {
el.click(function() {
getLocation(el);
});
}
function getLocation(el) {
el.html("Loading....");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) { showPosition(el, position); });
} else {
el.html("Geolocation is not supported by this browser.");
}
}
function showPosition(el, position) {
el.html("Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude);
}
</script>
</body>
</html>
通過這個'$( 「#BTN」 +(增量-1)),刪除();'你已經與ID'BTN1/2/3刪除特定鍵.. 。' –
它被用來刪除最後一個按鈕等,如果我可以有一個功能,使我可以刪除我選擇的那個按鈕,那將會很好 –
你將如何定位你想要刪除的特定按鈕?通過ID? –