我有一个页面,在运行时我会根据情况添加一些控件。
我在我的表中为mouseover
的{{1}}事件编写了此代码:
TDs
并且工作正常。但是根据jQuery doc:
不推荐使用.live()方法。使用.on()附加事件处理程序。旧版jQuery的用户应该使用.delegate()而不是.live()。
我像这段代码一样使用$(".TableEntry .EntryCell").live("mouseover", function () {
var parent = $(this).parent();
parent.css("background-color", "C4F7C3");
});
或on
,但它不起作用:
delegate
如何使用$(".TableEntry .EntryCell").on("mouseover", function () {
var parent = $(this).parent();
parent.css("background-color", "C4F7C3");
});
或on
动态添加事件处理程序?
答案 0 :(得分:6)
您的命名变量par
无效应为parent
..
您需要找到一个将事件处理程序绑定到..
的持久父级要模拟.live
,您需要将其添加到$('body')
所以
$("body").on("mouseover", '.TableEntry .EntryCell', function () {
var parent = $(this).parent();
parent.css("background-color", "C4F7C3");
});
要将其用作delegate
,您需要找到一个常见的持久父级并绑定它...
$("_persisted_parent_id_tag_etc_").on("mouseover", '.TableEntry .EntryCell', function () {
var parent = $(this).parent();
parent.css("background-color", "C4F7C3");
});
所以你绑定到$('...')
中的元素,处理程序应用于你传递的选择器作为第二个参数(如果你传递一个)
答案 1 :(得分:1)
我认为你需要在你的颜色值前添加一个#
:#C4F7C3
否则你将看不到鼠标悬停的任何结果。