我有一个带有以下标记的ASP.NET用户控件:
<div>
<script id="myTemplate" type="text/x-jquery-tmpl">
<table id="t1"><tr>...<td class="myclass"><span>First Name:</span></td>...<\tr> <\table>
</script>
<table id="t2"><tr>...<td class="myclass"><span>First Name:</span></td>...<\tr> <\table>
<\div>
我想用class =“myClass”为所有类触发hover()。我已经下面的代码:
$(document).ready(function() {
$(".myClass").hover(
function() {
alert('in...');
},
function() {
alert('out...');
});
}
问题是.hover()触发表“t2”中的td元素但不触发“t1”。有人可以帮忙吗?
答案 0 :(得分:2)
.hover静态添加事件处理程序。
尝试
$(".myClass").live("hover",
function() {
alert('in...');alert('out...');
}
}
答案 1 :(得分:1)
“t1”在jQuery模板中。我想当你的ready函数被执行时,这个模板还没有被插入到DOM中。因此它不存在,也没有附加事件。您有两种可能性:要么在插入模板后触发函数,要么使用将事件绑定到所有现有和未来元素的"delegate"-function of jQuery。
答案 2 :(得分:0)
可能是因为t1的HTML是在脚本标签中?或者jQuery模板是如何工作的?