Troubleshooting a CSS problem with iPad browsers

I have a specific issue related to CSS on iPad. I am working with a set of sublinks and have defined the styles in CSS as follows:

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

In my JavaScript code, I have the following definitions:

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

My concern is that when clicking on any sublink on the iPad, it seems to apply the :hover state CSS to it. Is there a way to change or override this behavior?

Before suggesting basic fixes, please note that I have already explored some options without success.

I understand that there is no hover event on iPads, but nonetheless, the :hover state class is being applied when links are clicked in my case.

Removing the :hover definition from the CSS is not an option for me due to compatibility issues with desktop browsers. So, my primary question remains: how can I prevent the :hover state from being triggered when a link is clicked?

Here is the updated code snippet I've 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));
                });
    

Answer №1

When using Safari mobile, events are handled in a specific order: upon pressing and releasing the screen, the following sequence occurs:

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

It's important to note that onmouseout (which signifies the end of the :hover state) will not be triggered unless you click elsewhere. This discovery took some time for me to uncover.

To work around this issue, you can implement the following code to activate your links before the onmouseover event takes place.

        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
                        }
                }
        }

Note: This snippet can also be used to prevent Safari from opening when in "standalone mode" (webapp) and clicking on links. This was the primary purpose behind creating this function. Additionally, referencing the event handling guide in the official documentation may provide further insight.

Answer №2

When a user taps on an element, the :hover styles are triggered and various mouse events such as mouseover, mousemove, mousedown, mouseup, and click are fired in sequence.

:Hover effects can be seen even on iOS devices. It is not clear if iOS supports :focus or :active, but you could try using those to revert the changes made by :hover.

Alternatively, you can use JavaScript to call the .blur() method (not tested).

Answer №3

Check out the

document.styleSheets[0].deleteRule()
method, and use it to remove any rule that includes ":hover" related to links. You can find more information in this documentation and see an example of how to use it here.

It's important to note that pseudo element scripting may not work reliably on all browsers, so make sure to iterate through the stylesheet and delete any "a:hover" rules.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transforming hover interactions into onclick events involves adjusting the CSS and JavaScript

Is there a way to convert this hover menu into an onclick menu? <div id='cssmenu'> <ul> <li class='has-sub'><a href='#'><span>Users</span></a> <ul> ...

Error: Unable to execute $(...).stellar since it is not a recognized function

Having some trouble implementing the stellar plugin. I've included all the necessary js, but keep getting an error in the dev tools console: 'Uncaught TypeError: $(...).stellar is not a function'. Here's what I have in the head tags: ...

emphasizing the specific text being searched for within the page

Looking for a solution to search values and highlight matching text words on a page? Let me help you with that. $(function() { var tabLinks = $('.nav > li'), tabsContent = $('.tab-content > div'), ...

The jQuery .load function doesn't seem to be functioning properly when used within a partial view displayed inside a modal popup

I am facing an issue with loading a partial view via ajax call in my main view. The modal popup is displayed when the "AddSkillBtn" button is clicked, but I need to trigger an ajax call when the partial view is loaded. However, using .on 'load' d ...

What could be the reason for XMLHttpRequest's readystate being something other than 4?

Currently delving into the realm of AJAX for the first time, and endeavoring to construct a basic application that computes the required grade on a final exam based on past assessment results. The user keys in their grades into the form, subsequent to whi ...

Gather image origins from a website, even if img tags are inserted through javascript or other methods during the page's rendering

I am looking to extract the URLs of all the images from a web page using C# and asp.net. Currently, I am utilizing: WebClient client = new WebClient(); string mainSource = client.DownloadString(URL); Afterwards, I am scanning the mainSource string for i ...

Tips on obtaining the screen resolution and storing it in a PHP variable

Hey there! I've run into a bit of a roadblock that I'm struggling to overcome. I know that I need to incorporate some JavaScript to solve this issue, but I'm having trouble grasping how to do so. Here's the script I'm working with: ...

Is there a more "Angular-esque" approach to implementing this (inter-element communication)?

I have created a custom directive that will automatically add an asterisk to the label of any input field marked as required. The following is my link function with detailed comments: // This is what the DOM structure looks like: // <label id="label-1" ...

What is the best method to position images in the same way as seen in the screenshot

Any tips on aligning images shown in the screenshot? Please note that the images will be from the backend. https://i.stack.imgur.com/LnYLM.jpg I experimented with code to position images using top, right, left, and bottom properties, but it becomes cumb ...

Loading an HTML template dynamically into a pre-loaded div element

I need to dynamically load an HTML template into my index.html file. Now, I am looking to load another HTML template into the previously loaded template content. To clarify further: The index is loaded with a dashboard template, and the dashboard contains ...

CSS issue: Font size

Can someone help me out with a CSS issue that has been tripping me up? I've been working with CSS for three years now, and this particular problem is stumping me. I have defined some styles in my CSS file that should be applied to the content of my w ...

Unable to generate a fresh database entry through form submission

I have designed User and Pairings models as shown below: class User < ActiveRecord::Base enum role: [:student, :supervisor, :admin] has_many :students, class_name: "User", foreign_key: "supervisor_id" belongs_to :supervisor, ...

Utilize the power of dual API integration in a single request (multi-scope capability)

While there may not be a definitive answer to this question, I want to ensure that my situation cannot be resolved in any way. The crux of my application (and consequently the issue) is that each user possesses their own unique database and does not have ...

What is the process for creating an Account SAS token for Azure Storage?

My goal is to have access to all containers and blobs in storage. The Account SAS token will be generated server-side within my Node.js code, and the client will obtain it by calling the API I created. While Azure Shell allows manual creation of a SAS toke ...

Using v-model in a child component and setting v-model within a child component in a Vue project

How can I simplify this code? Ensure that the button also updates the localValue of the child component. Vue.component('my-input', { template: ` <div> <b>My Input:</b> <br> localValue: {{ localValue } ...

What is the maximum duration we can set for the Ajax timeout?

I am facing a situation where an ajax request can take between 5-10 minutes to process on the server side. Instead of continuously polling from JavaScript to check if the request is completed, I am considering making just one ajax call and setting the tim ...

jQuery: Select the parent element of a focused form input and apply styling

Recently, I encountered a situation where I have a form containing a DIV along with 3 INPUTS, each enclosed within a LABEL element. My goal is to alter the background image of the DIV whenever an INPUT receives focus. Due to limitations in navigating up t ...

Six Material-UI TextFields sharing a single label

Is there a way to create 6 MUI TextField components for entering 6 numbers separated by dots, all enclosed within one common label 'Code Number' inside a single FormControl? The issue here is that the label currently appears only in the first tex ...

I'm having trouble grasping the concept of this asynchronous behavior

After reviewing the output provided, my initial expectation was for working to be displayed between the lines of begin and end. Here is the rule: Is there a possible solution to achieve this outcome without modifying the app.js file? app.js const se ...

The SVG image does not display in browsers other than Internet Explorer (IE)

I am encountering an issue with adding a menu toggle image in SVG format to my HTML. Unfortunately, the image is not displaying as expected. Here are screenshots for comparison between Explorer and Chrome: https://i.stack.imgur.com/74sqh.png https://i. ...