防止 iPad 中的悬停状态

发布于 2024-11-26 12:54:36 字数 902 浏览 1 评论 0原文

我在 iPad 上查看的页面有一些子链接...现在,当我单击任何这些链接时,我希望相应的链接处于活动状态,并防止

$("#leftNav  a").mouseleave(); 
$("#leftNav  a").mouseout(); 
$("#leftNav  a").trigger("mouseout"); 
$("#leftNav  a").trigger("mouseleave");

我尝试过

var a=document.getElementsByClassName("searchBySub");                 
            for(var i=0;i<a.length;i++)                 
            {                         
            a[i].ontouchstart=function(e){
            console.log("ontouchstart5");
            e.preventDefault(); 
            return false;}                 
            }

任何链接出现悬停状态在你说之前,iPad 上没有悬停,我知道这一点......但它确实应用了 :hover 样式;

当用户通过单击某个元素来聚焦该元素时,:hover 应用样式以及 mouseover、mousemove、mousedown、mouseup 并点击事件触发(始终;并且按该顺序)。

所以问题是如何防止某些链接的悬停状态(仅适用于 iPad)? 我无法从 CSS 中删除 :hover 定义,因为相同的 CSS 也用于桌面。

I have some sublinks for a page viewed on the iPad...Now when I click on any of those links, I want the corresponding link to be active and also prevent the hover state from any of the links

I have tried

$("#leftNav  a").mouseleave(); 
$("#leftNav  a").mouseout(); 
$("#leftNav  a").trigger("mouseout"); 
$("#leftNav  a").trigger("mouseleave");

I have also tried

var a=document.getElementsByClassName("searchBySub");                 
            for(var i=0;i<a.length;i++)                 
            {                         
            a[i].ontouchstart=function(e){
            console.log("ontouchstart5");
            e.preventDefault(); 
            return false;}                 
            }

Before you say, there is no hover on iPad, I am aware of that....But it does indeed apply the :hover styles as per;

When the user focuses on an element by single-tapping it, the :hover
styles are applied and the mouseover, mousemove, mousedown, mouseup
and click events fire (always; and in that order).

So question is how do I prevent the hover state on some links, only for the iPad ?
I cannot remove the :hover definition from the CSS as the same CSS is also used for desktop.

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

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

发布评论

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

评论(3

远山浅 2024-12-03 12:54:36

您可以使用 CSS 媒体查询专门为 iPad 设置样式表并覆盖之前的 :hover 定义:

<link rel="stylesheet" href="#" media="only screen and (device-width: 768px)" />

我假设对于 iPad 2,您将需要另一个具有双倍设备宽度的媒体查询。

You can use CSS media queries to set a stylesheet specifically for the iPad and override your :hover definitions from earlier:

<link rel="stylesheet" href="#" media="only screen and (device-width: 768px)" />

I assume that for the iPad 2 you will need another media query with double the device-width.

难理解 2024-12-03 12:54:36

注意:未经测试

首先创建一个函数来检测用户是否正在使用 iPad。像这样:

$.isiPad = function(){
  return (navigator.platform.indexOf("iPad") != -1);
}

如果使用 iPad,请删除所有链接上的 Hoovering 类:

$(document).ready(function(){
   if($.isiPad())
   {
     $("a").hover(function(){
        $(this).addClass('your_a_class_when_not_hoovering');
     });
   }
});

如果您只想在某些链接上应用此功能,您可以这样做:

 $(document).ready(function(){
   var apply_to_links = ['link_one_class_or_id','link_two_class_or_id']

  if($.isiPad())
   {
     $.each(apply_to_links,function(){
       $(this).hover(function(){
        $(this).addClass('your_a_class_when_not_hoovering');
       });
     })
   }
});

就像我说的,它未经测试,但我认为您明白了。
希望有帮助!

Note: Not tested

First off create a function for detect if the user is using iPad. Like so:

$.isiPad = function(){
  return (navigator.platform.indexOf("iPad") != -1);
}

remove hoovering class on all links if using iPad:

$(document).ready(function(){
   if($.isiPad())
   {
     $("a").hover(function(){
        $(this).addClass('your_a_class_when_not_hoovering');
     });
   }
});

If you only want to apply this on some links you culd do this:

 $(document).ready(function(){
   var apply_to_links = ['link_one_class_or_id','link_two_class_or_id']

  if($.isiPad())
   {
     $.each(apply_to_links,function(){
       $(this).hover(function(){
        $(this).addClass('your_a_class_when_not_hoovering');
       });
     })
   }
});

Like i said its not tested but ill think you get the point.
Hope it helps!

枫林﹌晚霞¤ 2024-12-03 12:54:36

不要在有问题的情况下使用 :hover 。使用一个类('.hover')并将该类设置为 ..hover,

$(document).ready(function(){
   $("a").hover(function(){
      $(this).addClass('hover');
   }, function(){
     $(this).removeClass('hover');
});

正如您所指出的,这在 ipad 上不起作用,这正是您想要的:-)

Don't use :hover in the situations where its a problem. Use a class ('.hover') and set that class on .. hover

$(document).ready(function(){
   $("a").hover(function(){
      $(this).addClass('hover');
   }, function(){
     $(this).removeClass('hover');
});

as you noted, that doesnt work on an ipad, which is exactly what you want :-)

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