假設我使用jQuery將新內容加載到特定的DIV元素中。如果我現在想從該DIV元素中捕獲事件,那麼我認爲DOM必須以某種方式更新?處理這個問題的最佳方法是什麼?在jQuery中插入後更新DOMD
編輯:這是我的意思的例子:
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script>
$(document).ready(function(){
$(".edit").click(function(){
$(this).parent().load("edit");
});
$(".view").click(function(){
$(this).parent().load("view");
});
});
</script>
</head>
<body>
<div class='someid'>
<input type='submit' class='edit' value='Edit'>
</div>
</body>
</html>
,其中在同級別文件edit
包含
<input type='submit' class='view' value='View'>
和文件 '視圖' 包含
<input type='submit' class='edit' value='Edit'>
如果您嘗試了這一點,您會看到當您第一次按下Edit時,該按鈕將更改爲View,但那麼它不會再改變。爲什麼是這樣?
如果您將新內容加載到DIV元素中,那麼*與更新DOM的效果相同,不是嗎? – ChrisW 2009-10-11 14:06:53
我的意思是如果我使用例如$(this).load('getcontent'),並且該內容包含新的DIV元素以及之前未定義的新ID,我似乎無法使用jQuery選擇這些新元素 – astrofrog 2009-10-11 14:08:45