2011-04-29 32 views
0

刪除元素我有HTML這樣的:如何使用特定的ID

<html> 
    <input type='button' name='button1' id='button1'> 
    <input type='button' name='button2' id='button2'> 
</html> 

我想這樣做,當用戶點擊BUTTON2,應該刪除Button1的元素,並顯示在其地點的消息。它會看起來像這樣button2點擊事件。

<html> 
    Button1 Removed 
    <input type='button' name='button2' id='button2'> 
</html> 

回答

1

如果使用jQuery的,你可以這樣做:

$('#button2').click(function() { 
    $('#button1').replaceWith('Button1 Removed') 
}); 
1

使用jQuery帶有嵌入式功能還可以做其他的事情。

$('#button2').click(function() { $('#button1').remove().html('<p>Button 1 Removed</>') }); 

記得它的好做法,如果你使用jQuery,那麼這個解決像等

1
$(document).ready(function(){ 
    $("#button1").click(function(){ 
     $("#button1").replaceWith("<div>Hi der</div>"); 
    }); 
}); 
1

一些標籤來始終將文字:

$('#button2').click(function(){ 
    $('#button1').replaceWith('<p>Button1 Removed</p>'); 
}); 
1
$('#button1').click(function() { 
$('this').remove().html('<p>Button 1 Removed</>') 
alert('button1 removed'); 
}); 
1
$(document).ready(function(){ 
    $("#button2").click(function(){ 
     $("#button1").replaceWith("Button1 Removed"); 
    }); 
});