我的实际问题是,jQuery的.live()方法无法工作。
这是我使用它的代码:
jQuery.fn.sb_animateMenuItem = function()
{
var mousehoverColor = '#0089F7';
var duration = 250;
return this.each(function()
{
var originalColor = $(this).css('background-color');
$(this).live('mouseover', function()
{
this.style.cursor = 'pointer';
$(this).animate().stop();
$(this).animate(
{
backgroundColor: mousehoverColor
}, duration);
});
$(this).live('mouseout', function()
{
this.style.cursor = 'default';
$(this).animate(
{
backgroundColor: originalColor
}, duration);
});
});
};
这个代码片段可以通过以下方式在另一个页面中使用:
<script type="text/javascript" src="ui/js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="ui/js/jquery-ui-1.8.1.custom.min.js"></script>
<script type="text/javascript" src="ui/js/color.js"></script>
<script type="text/javascript" src="engine/js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="ui/js/ui.js"></script>
<script type="text/javascript">
// UI effects
$(document).ready(function()
{
$('button').sb_animateButton();
$('input').sb_animateInput();
$('.top_menu_item').sb_animateMenuItem();
$('.top_menu_item_right').sb_animateMenuItem();
$('.left_menu_item').sb_animateMenuItem();
});
</script>
由于我的网站使用 AJAX 请求,所以我在第一个代码片段中使用了 .live 方法,但是当我加载页面时,按钮/输入标签没有应用效果。
如果我删除 .live 方法并使用“普通”方式,在第一个代码片段中定义的 UI 效果将被应用,但只有在任何 AJAX 请求之前加载的元素才会受到影响。加载 AJAX 请求后的元素不受第一个代码片段的影响(尽管它们具有相同的选择器)。
感谢您的帮助。