将鼠标悬停在由 ajax 加载或获取函数填充的元素上时出现问题
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以使用 $.fn.live()
http://api.jquery.com/live/
更新
还应该注意的是,另一个合适的解决方案是:
http://api.jquery.com/delegate/
You can also use $.fn.live()
http://api.jquery.com/live/
Update
It should also be noted that another appropriate solution is:
http://api.jquery.com/delegate/
我相信你的悬停是在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(){});
在任何 ajax 调用之后,您必须重新调用元素上的悬停函数
After any ajax call you have to re-call the hover function on the elements
尝试在 $(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