鏈接#loadContent將加載tree.html。在成功加載內容後,腳本將重新初始化tree.html中的一些函數。但是,我無法使加載內容中的.click事件發揮作用。Jquery嵌套div .click事件
的Index.html
<a href="#" id="loadContent">Load</a>
<script type="text/javascript">
$(function() {
$("#loadContent").click(function() {
$.ajax({
url: "tree.html"
,success: function(data) {
$('#result').html(data);
$("#demo_1").tree({
rules : {
use_max_children : false,
use_max_depth : false
},
callback : {
onmove : function (NODE,REF_NODE,TYPE,TREE_OBJ,RB) {
alert(TREE_OBJ.get_text(NODE) + " " + TYPE + " " + TREE_OBJ.get_text(REF_NODE));
}
}
});
}
});
});
});
</script>
<script type="text/javascript" class="source">
$(function() {
$.tree.drag_start = function() {
$("#log").append("<br />Drag start ");
};
$.tree.drag = function() {
$("#log").append(" .");
};
$.tree.drag_end = function() {
$("#log").append(" Drag end<br />");
};
$("#demo_1").tree({
rules : {
use_max_children : false,
use_max_depth : false
},
callback : {
onmove : function (NODE,REF_NODE,TYPE,TREE_OBJ,RB) {
alert(TREE_OBJ.get_text(NODE) + " " + TYPE + " " + TREE_OBJ.get_text(REF_NODE));
}
}
});
$("#demo_2").tree({
rules : {
use_max_children : false,
use_max_depth : false
},
callback : {
onmove : function (NODE,REF_NODE,TYPE,TREE_OBJ,RB) {
alert(TREE_OBJ.get_text(NODE) + " " + TYPE + " " + TREE_OBJ.get_text(REF_NODE));
}
}
});
});
</script>
<div class="demo" id="demo_2">
<ul>
<li id="phtml_1" class="open"><a href="#"><ins> </ins>Root node 1</a>
<ul>
<li id="phtml_2"><a href="#"><ins> </ins>Child node 1</a></li>
<li id="phtml_3"><a href="#"><ins> </ins>Child node 2</a></li>
<li id="phtml_4"><a href="#"><ins> </ins>Some other child node with longer text</a></li>
</ul>
</li>
<li id="phtml_5"><a href="#"><ins> </ins>Root node 2</a></li>
</ul>
</div>
<div id="result"></div><br>
<div id="log"></div>
Tree.html
<div class="demo" id="demo_1">
<ul>
<li id="phtml_1" class="open"><a href="#"><ins> </ins>Root node 1</a>
<ul>
<li id="phtml_2"><a href="#"><ins> </ins>Child node 1</a></li>
<li id="phtml_3"><a href="#"><ins> </ins>Child node 2</a></li>
<li id="phtml_4"><a href="#"><ins> </ins>Some other child node with longer text</a></li>
</ul>
</li>
<li id="phtml_5"><a href="#"><ins> </ins>Root node 2</a></li>
<li><a class="preset_text" id="1">model 1</a> </li>
<li><a class="preset_text" id="2">model 2</a></li>
</ul>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('.preset_text').click(function(){
var target = $(this).attr("id");
alert(target);
});
});
</script>
在tree.html,我無法得到警報(目標)。但是,如果我將此部分從tree.html中的「div#demo_1」中移出,則可以接收警報(目標)。
<a class="preset_text" id="1">model 1</a>
<a class="preset_text" id="2">model 2</a>
如何才能檢測在div demo_1中點擊的項目? 感謝
究竟什麼都是' 's for? – Eric 2010-05-27 10:50:02
如果你移除'$(document).ready()'(不知道當你插入到一個現有的DOM中時這個事件是否被觸發),它會起作用嗎?儘管jQuery試圖猜測返回的數據類型,但也許您應該在Ajax調用中明確聲明數據類型:'dataType:html' – 2010-05-27 10:52:03