为什么这个 jQuery悬停() 不起作用?

发布于 2024-12-23 06:07:09 字数 485 浏览 5 评论 0原文

我在名为 uptoppage 的自定义 HTML 标记内有 2 个表单。

当文档加载时,我向上滑动保存表单的元素。一旦有人再次将鼠标悬停在父元素上,我希望它向下滑动。相反,什么也没有发生。

这是我的标记:

<uptoppage>controller....
  <uptpc>
    <form>.....</form>
    <form>......</form>
  </uptpc>
<uptoppage>

这是我正在使用的 jQuery:

$("uptoppage").hover(function() {
  $('uptpc', this).slideDown();
}, function() {
  $('uptpc', this).slideUp();
});

I have 2 forms inside a custom HTML tag called uptoppage.

When the document loads, I slide up the element that holds the forms. Once someone hovers their mouse over the parent element again, I want it to slide down. Instead, nothing happens.

Here's my markup:

<uptoppage>controller....
  <uptpc>
    <form>.....</form>
    <form>......</form>
  </uptpc>
<uptoppage>

Here's the jQuery I'm using:

$("uptoppage").hover(function() {
  $('uptpc', this).slideDown();
}, function() {
  $('uptpc', this).slideUp();
});

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

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

发布评论

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

评论(2

微暖i 2024-12-30 06:07:09

留意大小变化

我认为您使用 hover() 或回调函数没有任何问题。我修改了你的例子;我看到的主要奇怪行为是,如果子元素被隐藏,这会导致父元素变得如此之小,以至于它从鼠标下方移出。

换句话说,jQuery 正在观察您是否将鼠标悬停在某个元素上。如果该元素正在增大和缩小,它可能会改变您是否仍在其上方,触发悬停,然后悬停,等等,而无需移动鼠标。因此,请确保父元素中有一些其他内容和/或它具有 CSS 指定的大小。

检查您的标记

我也同意第一条评论:如果可以避免,则不应使用自定义元素。我给父元素一个边框,这样我就可以看到它有多大,但是直到我将它设为带有 id 的 div 而不是自定义元素类型后,样式才显示出来。

如果可能的话,使用 id 和类来标识标准元素,而不是使用自定义元素。您的标记越不寻常,您对问题就越不确定 - “这是我的 JS 的问题,还是浏览器因我的自定义 HTML 而阻塞?”

Watch for size changes

I don't see anything wrong with your use of hover() or your callback functions. I tinkered with your example; the main weird behavior I saw was if the child element is hidden and that causes the parent to becomes so small that it moves out from under the mouse.

In other words, jQuery is watching to see whether you're hovering over an element. If that element is growing and shrinking, it may change whether you're still over it, triggering the hover out, then the hover in, etc, without your mouse moving. So make sure that you have some other content in the parent element and/or it has some size given to it by CSS.

Check your markup

I'd also agree with the first comment: you shouldn't use custom elements if you can avoid it. I gave the parent element a border so that I could see how big it was, but the style didn't show up until I made it a div with an id instead a custom element type.

If at all possible, use ids and classes to identify standard elements instead of using custom ones. The more unusual your markup is, the more uncertain you'll be about problems - "Is this a problem with my JS, or is the browser choking on my custom HTML?"

£噩梦荏苒 2024-12-30 06:07:09
<script type="text/javascript">
     $(document).ready(function(){ 
           $('form').hover(function(){
             $('form').slideUp();
             $(this).slideDown();
           });
        });
</script>
<script type="text/javascript">
     $(document).ready(function(){ 
           $('form').hover(function(){
             $('form').slideUp();
             $(this).slideDown();
           });
        });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文