iPad 上的 CSS 问题

发布于 2024-11-26 09:10:20 字数 1502 浏览 2 评论 0原文

我在 iPad 上有一个非常简单的 CSS 相关问题。 我有一组子链接,每个子链接在 CSS 中定义为:

#leftNav .searchBySub {...}
#leftNav a.searchBySub:hover {...}
#leftNav .searchBySubClicked {...}

我有一个 JS ,我已经在其中定义了

$("#leftNav  a.searchBySub").live("touchstart",function(){...}
$("#leftNav  a.searchBySub").live("click",function(){...}

现在我的问题是,当我单击 iPad 上的任何子链接时,为什么它会对其应用 :hover 状态 CSS?我也可以以某种方式改变这种行为吗?

在提出任何简单/基本建议之前,请仔细阅读问题,因为我已经尝试过基本修复。

此外,在您说之前,iPad 上没有悬停事件,我知道这一点,但就我而言,它确实如此单击链接时应用 :hover 状态类。

我知道您可能会说只需从 CSS 中删除 :hover 定义,但不幸的是我不能这样做,因为桌面浏览器也使用相同的 CSS(甚至对于媒体查询,它没有帮助)

所以我的主要问题是单击链接时如何覆盖:悬停状态?

更新...

下面是我尝试过的代码;

    $("#leftNav  a.searchBySub").live("touchstart",function(){
                var a=document.getElementsByClassName("searchBySub");                 
                for(var i=0;i<a.length;i++)                 
                {                         
                a[i].ontouchstart=function(e){
 window.location=this.getAttribute("href");
                console.log("ontouchstart2");
                e.stopPropagation();                                 
                e.preventDefault(); 
                return false;}                 
                }
                $("#leftNav  a.searchBySub").removeClass("searchBySubClick");
                $(this).addClass("searchBySubClick");
                clickEvent($(this));
            });

I have a very simple CSS related question on iPad.
I have a set of sublinks each defined in CSS as;

#leftNav .searchBySub {...}
#leftNav a.searchBySub:hover {...}
#leftNav .searchBySubClicked {...}

And I have a JS where I have defined

$("#leftNav  a.searchBySub").live("touchstart",function(){...}
$("#leftNav  a.searchBySub").live("click",function(){...}

Now my question is when I click on any of the sublinks on the iPad, why does it apply the :hover state CSS to it ? Also can I change that behavior somehow ?

Please read the question carefully before making any simple/basic suggestions, as I have already tried the basic fixes..

Also before you say, there is no hover event on the iPad, I am aware of that, but in my case, it does apply the :hover state class when the link is clicked..

I know you might say just remove the :hover definition from the CSS, but unfortunately I cannot do that, as the same CSS is used by the desktop browsers as well (and even with media queries, it does not help)

So my main question is how do I override the :hover state when the link is clicked ?

Updated ...

Below is the code I have tried;

    $("#leftNav  a.searchBySub").live("touchstart",function(){
                var a=document.getElementsByClassName("searchBySub");                 
                for(var i=0;i<a.length;i++)                 
                {                         
                a[i].ontouchstart=function(e){
 window.location=this.getAttribute("href");
                console.log("ontouchstart2");
                e.stopPropagation();                                 
                e.preventDefault(); 
                return false;}                 
                }
                $("#leftNav  a.searchBySub").removeClass("searchBySubClick");
                $(this).addClass("searchBySubClick");
                clickEvent($(this));
            });

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

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

发布评论

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

评论(3

旧人哭 2024-12-03 09:10:20

Safari 手机按特定顺序处理该事件:当您按下然后释放屏幕时,会发生以下情况:

ontouchstart - onmouseover/:hover - mousedown - mouseup - click/:active

请注意,onmouseout:hover 状态结束)不会除非您单击其他地方,否则会调用。我花了一段时间才弄清楚这一点。

作为解决方法,您可以使用此类代码在 onmouseover 事件发生之前触发您的链接。

        function ipadLinksHack()//Call it onload
        {
                var a=document.getElementsByTagName("a");
                for(var i=0;i<a.length;i++)
                {
                        //Note to self: ontouchstart and not onclick! :)
                        a[i].ontouchstart=function(e)
                        {
                                window.location=this.getAttribute("href");
                                e.stopPropagation();
                                e.preventDefault();
                                return false;//Prevents the link default behavior
                        }
                }
        }

备注:当您处于“独立模式”(又名网络应用程序)并点击链接时,您还可以使用此代码段来防止打开 Safari。这是这个函数的主要目的。
另外,请阅读事件处理指南 官方文档可能会有所帮助。

Safari mobiles handles the event on a particular order: when you press then release screen here is what happens:

ontouchstart - onmouseover/:hover - mousedown - mouseup - click/:active

Notice that onmouseout (end of the :hover state) won't be called unless you click elsewhere. Took me a while to figure that out.

As a workaround you can use this kind of code to trigger your links before the onmouseover event occurs.

        function ipadLinksHack()//Call it onload
        {
                var a=document.getElementsByTagName("a");
                for(var i=0;i<a.length;i++)
                {
                        //Note to self: ontouchstart and not onclick! :)
                        a[i].ontouchstart=function(e)
                        {
                                window.location=this.getAttribute("href");
                                e.stopPropagation();
                                e.preventDefault();
                                return false;//Prevents the link default behavior
                        }
                }
        }

Remark: you can also use this snippet to prevent the opening of safari when you're in "standalone mode" (aka webapp) and click on links. That was the main purpose of this function.
Also, reading the event handling guide on the official doc might help.

没有伤那来痛 2024-12-03 09:10:20

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

所以 :hover 即使在 iOS 上也适用。不确定 iOS 是否支持 :focus (或者可能是 :active),但您可以使用它来“撤消”由 :hover 所做的更改。

或者使用JS调用.blur()(未经测试)。

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 :hover is applied even on iOS. Not sure if iOS supports :focus (or maybe :active), but you could use that one to 'undo' the changes made by :hover.

Or use JS to call .blur() (untested).

花间憩 2024-12-03 09:10:20

查看 document.styleSheets[0].deleteRule() 函数,然后简单地删除相对于链接包含“:hover”的每个规则。
这是一些文档一些用法示例

请小心,因为伪元素脚本在大多数浏览器上似乎不可靠,因此只需循环样式表并删除“a:hover”规则即可。

Have a look about the document.styleSheets[0].deleteRule() function, and simply delete every rule containing ":hover" relative to a links.
Here is some doc and some example of usage.

Be careful as pseudo element scripting seems unreliable on most browsers, so just loop through the stylesheet and delete "a:hover" rules.

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