Jquery 无法悬停追加 html

发布于 2024-08-17 07:01:33 字数 466 浏览 4 评论 0原文

我通过 jquery.append 将 HTML 附加到 DOM 中 - 我的脚本如下(请原谅糟糕的代码)

myDiv = $("<div class='bottomright' title="+msgID+">"+msgTitle+msgContent+
"</div>").appendTo(document.body).fadeOut(0).slideDown('fast');
shown.push(msgID);

是否有不同的方法来解决悬停或附加问题(因为查看 firebug,似乎 div 进入了那里)好的,它们完美地显示)以便我可以使用悬停功能?

当我说我无法使用它时,我的意思是它实际上什么也没做,我写了以下内容但没有任何反应:

$(".bottomright").hover(function(){
    alert("text")
})

I am appending HTML into the DOM via jquery.append - my script is as follows (please excuse the crappy code)

myDiv = $("<div class='bottomright' title="+msgID+">"+msgTitle+msgContent+
"</div>").appendTo(document.body).fadeOut(0).slideDown('fast');
shown.push(msgID);

is there a different way to address either the hover or the appending (because looking at firebug, it seems the divs get in there ok, and they show up perfectly) so that I can use the hover function?

When I say I am unable to use it, I mean it actually does nothing, I have written the following and nothing happens:

$(".bottomright").hover(function(){
    alert("text")
})

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

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

发布评论

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

评论(4

寂寞美少年 2024-08-24 07:01:33

hover 采用两个函数作为参数:

第一个参数是当元素从鼠标获得焦点时要执行的函数。
当元素失去鼠标焦点时会触发第二个。

-但-
由于您是动态生成这些元素,因此需要使用 live

live 无法与 hover 一起使用,因此您被迫执行此操作:

$( '.bottomright' ).live( 'mouseover',  function() { alert( "in" ); } )
                   .live( 'mouseout', function() { alert( "out" ); } );

hover takes two functions as arguments:

the first argument is a function that is to be executed when the element gains focus from the mouse.
the second is fired when the element loses focus from the mouse.

-BUT-
since you are dynamically generating these elements, you need to use live:

live does not work with hover, so you are forced to do this:

$( '.bottomright' ).live( 'mouseover',  function() { alert( "in" ); } )
                   .live( 'mouseout', function() { alert( "out" ); } );
甜是你 2024-08-24 07:01:33

您需要使用 live() 函数,因为您的 div 似乎是动态生成的:

$('.bottomright').live('hover', function() { alert( "text" ); }, function() {} );

You need to use the live() function, as it appears that your div is being dynamically generated:

$('.bottomright').live('hover', function() { alert( "text" ); }, function() {} );
痴梦一场 2024-08-24 07:01:33

欢迎来到堆栈溢出!

只是一个提示 - 使用顶部的 10101 按钮格式化代码确实很有帮助。例如:

myDiv = $(""+msgTitle+msgContent+"").appendTo(document.body).fadeOut(0).slideDown('fast'); 
shown.push(msgID);

关于你的问题,我猜你的“bottomright”类对象可能不会占用任何空间。我注意到你的 fadeOut 和 SlideDown,也许还有其他原因导致 div 不占用任何空间? (检查萤火虫布局选项卡)。

另外,您是否在创建 div 后绑定悬停事件?如果是之前的话,可能就不行了。在 jQuery 1.3 中,他们添加了 live(type,fn) 来绑定所有未来的对象。

welcome to Stack Overflow!

Just a tip - it really helps to format code using the 10101 button at the top. For example:

myDiv = $(""+msgTitle+msgContent+"").appendTo(document.body).fadeOut(0).slideDown('fast'); 
shown.push(msgID);

In regards to your question, I'm guessing your object of class "bottomright" may not be taking up any space. I noticed your fadeOut and slideDown, maybe there is something else that is causing the div to not take up any space? (Check in firebug layout tab).

Also, are you binding your hover event after creating the div? If it is before, it might not work. In jQuery 1.3, they added live(type,fn) to bind all future objects also.

毁梦 2024-08-24 07:01:33

我想我找到了答案 - 我所做的就是将悬停调用移动到生成 div 的位置下,该位置位于 for 循环中。我进一步阅读了一下,它不起作用的原因是因为它在 DOM 加载时激活悬停,但没有 div 将其附加到 - 通过每次创建 div 时运行它,它似乎工作得很好。也许有更好的方法来做到这一点?不过还是谢谢你!我会尝试.live()

I think I found the answer - all I did was move the hover call to under where the div was being generated, which was in a for loop. I read a little further and the reason it wasn't working is because it was activating hover when the DOM loaded, but there was no div to attatch it to - by running it each time a div is made, it seems to work just fine. Maybe there is a better way of doing this?? Thank you though! I will try .live()

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