Php jQuery.live()不工作

Php jQuery.live()不工作,php,jquery,Php,Jquery,我的实际问题是.live()jQuery方法不起作用 这是我使用它的代码: jQuery.fn.sb_animateMenuItem = function() { var mousehoverColor = '#0089F7'; var duration = 250; return this.each(function() { var originalColor = $(this).css('background-color');

我的实际问题是.live()jQuery方法不起作用

这是我使用它的代码:

    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>

//用户界面效果
$(文档).ready(函数()
{
$('button').sb_animateButton();
$('input').sb_animateInput();
$('.top_菜单项').sb_animateMenuItem();
$('.top_菜单项_右').sb_animateMenuItem();
$('.left_菜单项').sb_animateMenuItem();
});
由于我的站点使用AJAX请求,我在第一个代码片段中使用了.live方法,但是当我加载页面时,效果不会应用于按钮/输入。。。标签

如果我删除.live方法并使用“正常”方式,则会应用第一次截取中定义的ui效果,但只会在任何AJAX请求之前加载元素。ajax请求之后加载的元素不受第一个代码段的影响(尽管它们具有相同的选择器)

谢谢您的帮助。

简而言之,您不能这样使用,它必须遵循某种选择器基础,这是:

不完全支持DOM遍历方法来查找要发送到
.live()
的元素。相反,应该始终在选择器之后直接调用
.live()
方法,如上面的示例所示

您在代表特定DOM元素的jQuery对象上调用
.live()
,相反,您需要获得插件运行的结果,如果有插件,当然这不是保证的,然后将其用于
.live
,如下所示:

 jQuery.fn.sb_animateMenuItem = function() {
    $(this.selector).live(.....)
如果你仔细想想,它是如何工作的?它侦听要冒泡的事件,检查目标是否与选择器匹配,如果匹配则执行(在上下文中,这是一个完整的其他讨论)…如果您执行了
$(doElement).live()
,它将检查哪个选择器以查看是否应该执行

我想你可以根据内部元素uuid来争论,这应该是可行的,但这只是一个,这样会减少浪费,所以不要这样做


更新:因为我想知道在不重复代码的情况下实现这一点的最简单方法,下面是一个插件版本,它可以根据是否存在可供使用的选择器进行选择或动态选择:

.

您可以使用.DELEGATE()代替.LIVE、.ON、.BIND


A真的很大+1。从这个答案中学到了很多+我也可以问你这个问题。嗯。。。我两种方法都试过了,但都不管用。我决定用另一种方式面对这个问题:@Silvio-什么不起作用?我提供了一个演示,展示了这一点,你应该解释什么不起作用。当你不知道问题所在时,很难给出解决方案…@Nick-我试着实现你的方法,但它似乎不适用于我的网站。我复制并粘贴到函数中,但得到的只是一个不起作用的效果。这就像没有效果一样。@Silvio-你有链接吗?你在这个问题之外还有别的东西影响它。例如,您正在调用的其他函数不在这里。谢谢,这在一个动态添加内容的实例中起作用,但没有触发(结果证明我做了其他错误的事情-错误地将表单嵌入表单中,但了解这一点很有意思)。
jQuery.fn.sb_animateMenuItem = function() {
  var mousehoverColor = '#0089F7';
  var duration = 250;
  var method = this.selector ? jQuery.fn.live : jQuery.fn.bind;
  method.apply(this, ['mouseover', function() {
     if(!jQuery.data(this, 'oColor'))
         jQuery.data(this, 'oColor', jQuery(this).css('background-color'));
     jQuery(this).stop(true).animate({ backgroundColor:mousehoverColor }, duration);
  }]);
  method.apply(this, ['mouseout', function() {
    jQuery(this).animate({ backgroundColor:jQuery.data(this, 'oColor') }, duration);
  }]);
  return this.css('cursor', 'pointer');
};
$("body").DELEGATE($(this),"click","myfunction")
$("body").DELEGATE($(this),"click",function () {
///code here
} )