将鼠标悬停在由 ajax 加载或获取函数填充的元素上时出现问题

发布于 2024-11-26 09:44:18 字数 1304 浏览 0 评论 0原文

我在 FF 和 Chrome 中遇到悬停功能问题。我有一个通过 ajax 动态填充的表。然后我尝试操作通过 ajax 发送的元素,但它在 FF 和 Chrome 中不起作用。以下是该页面的代码:

/** My page**/
$('#resourcelist tbody').load(inc_processplugin.asp?cmd=select&tc=listresource,function () {
             $('#divShowAddResource').hide();
      });

 $('.editable')
        .hover(function () {
            //alert('Am hovering');
            //change background of an editable element if mouse hover
            $(this).toggleClass('over-inline');
        })


/** The elements editable is the one that populate the tbody above. It's generated dynamically from inc_processplugin.asp Here is part of the incprocessplugin.asp that populate the tbody element**/

query = "SELECT * from t_cmsix_tc_resource WHERE isnull(resource_inactive,0)=0"
        objRs.Open query, objConn   
         Do while Not objRs.EOF     
            Response.Write("<tr>" & vbCrlf)          
            Response.Write("<td><span class=""editable"" id=""" & objRs("resource_id")  & """>" & objRs("resource_name") & "</span></td>" & vbCrlf) 
            Response.Write("</tr>" & vbCrlf) 
            objRs.MoveNext    
         Loop `

tbody 元素已正确填充,但动态生成的 .editable 元素上的悬停或任何其他操作不起作用。

请问有什么帮助吗????

I'm having an issue with hover function in FF and Chrome. I've a table that's populated dynamically through ajax. I'm then trying to manipulate the elements sends through ajax, but it's not working in FF and Chrome. Here is the code of the page:

/** My page**/
$('#resourcelist tbody').load(inc_processplugin.asp?cmd=select&tc=listresource,function () {
             $('#divShowAddResource').hide();
      });

 $('.editable')
        .hover(function () {
            //alert('Am hovering');
            //change background of an editable element if mouse hover
            $(this).toggleClass('over-inline');
        })


/** The elements editable is the one that populate the tbody above. It's generated dynamically from inc_processplugin.asp Here is part of the incprocessplugin.asp that populate the tbody element**/

query = "SELECT * from t_cmsix_tc_resource WHERE isnull(resource_inactive,0)=0"
        objRs.Open query, objConn   
         Do while Not objRs.EOF     
            Response.Write("<tr>" & vbCrlf)          
            Response.Write("<td><span class=""editable"" id=""" & objRs("resource_id")  & """>" & objRs("resource_name") & "</span></td>" & vbCrlf) 
            Response.Write("</tr>" & vbCrlf) 
            objRs.MoveNext    
         Loop `

The tbody element is correctly populated but the hover or any other actions on the .editable elements that are generated dynamically is not working.

Any help please????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雄赳赳气昂昂 2024-12-03 09:44:18

您还可以使用 $.fn.live()

http://api.jquery.com/live/

描述:将处理程序附加到当前和将来与当前选择器匹配的所有元素的事件。

更新
还应该注意的是,另一个合适的解决方案是:

$.fn.delegate()

http://api.jquery.com/delegate/

描述:根据一组特定的根元素,将处理程序附加到现在或将来与选择器匹配的所有元素的一个或多个事件。

You can also use $.fn.live()

http://api.jquery.com/live/

Description: Attach a handler to the event for all elements which match the current selector, now and in the future.

Update
It should also be noted that another appropriate solution is:

$.fn.delegate()

http://api.jquery.com/delegate/

Description: Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.

余罪 2024-12-03 09:44:18

我相信你的悬停是在ajax加载完成之前设置的。尝试将 .hover() 函数放入 $(document).ready(function(){});

I believe that your hover is set before ajax is done loading. Try putting the .hover() function in $(document).ready(function(){});

春夜浅 2024-12-03 09:44:18

在任何 ajax 调用之后,您必须重新调用元素上的悬停函数

    $('#resourcelist tbody').load(inc_processplugin.asp?cmd=select&tc=listresource,function () {
         $('#divShowAddResource').hide();
         $('.editable').hover(function () {
             $(this).toggleClass('over-inline');
         })
    })

  });

After any ajax call you have to re-call the hover function on the elements

    $('#resourcelist tbody').load(inc_processplugin.asp?cmd=select&tc=listresource,function () {
         $('#divShowAddResource').hide();
         $('.editable').hover(function () {
             $(this).toggleClass('over-inline');
         })
    })

  });
ま昔日黯然 2024-12-03 09:44:18

尝试在 $(document).ready() 事件处理程序中运行 jQuery 代码。当您附加悬停事件处理程序时,这些 td 元素可能不存在,因此它不起作用。

在确定元素已创建后,您还可以在 ajax 成功处理程序中运行悬停代码

try running your jQuery code in the $(document).ready() event handler. Those td elements may not exist when you're attaching the hover event handler, and therefore it's not working.

you can also run the hover code in the ajax success handler, after you're sure that the elements have been created

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文