How to write a:hover in inline CSS?

后端 未结 23 2547
孤城傲影
孤城傲影 2020-11-21 23:15

I have a case where I must write inline CSS code, and I want to apply a hover style on an anchor.

How can I use a:hover in inline CSS inside the HTML st

相关标签:
23条回答
  • 2020-11-21 23:36

    I agree with shadow. You could use the onmouseover and onmouseout event to change the CSS via JavaScript.

    And don't say people need to have JavaScript activated. It's only a style issue, so it doesn't matter if there are some visitors without JavaScript ;) Although most of Web 2.0 works with JavaScript. See Facebook for example (lots of JavaScript) or Myspace.

    0 讨论(0)
  • 2020-11-21 23:39

    Short answer: you can't.

    Long answer: you shouldn't.

    Give it a class name or an id and use stylesheets to apply the style.

    :hover is a pseudo-selector and, for CSS, only has meaning within the style sheet. There isn't any inline-style equivalent (as it isn't defining the selection criteria).

    Response to the OP's comments:

    See Totally Pwn CSS with Javascript for a good script on adding CSS rules dynamically. Also see Change style sheet for some of the theory on the subject.

    Also, don't forget, you can add links to external stylesheets if that's an option. For example,

    <script type="text/javascript">
      var link = document.createElement("link");
      link.setAttribute("rel","stylesheet");
      link.setAttribute("href","http://wherever.com/yourstylesheet.css");
      var head = document.getElementsByTagName("head")[0];
      head.appendChild(link);
    </script>
    

    Caution: the above assumes there is a head section.

    0 讨论(0)
  • 2020-11-21 23:39

    There is no way to do this. Your options are to use a JavaScript or a CSS block.

    Maybe there is some JavaScript library that will convert a proprietary style attribute to a style block. But then the code will not be standard-compliant.

    0 讨论(0)
  • you can write a code in various type

    first i can write this

    html

    <a href="https://www.google.com/" onMouseOver="this.style.color='red'"
            onMouseOut="this.style.color='blue'" class="one">Hello siraj</a> 
    

    css

    .one{
      text-decoration: none;
    }
    

    you can try another way

    html

    <a href="https://www.google.com/" class="one">Hello siraj</a>
    

    css

    .one{
    text-decoration: none;
    }
    
    .one:hover{
    color:blue;
    }
    
    .one:active{
    color: red;
    }
    

    you can also try hover in jquery

    java script

    $(document).ready(function(){
      $("p").hover(function(){
        $(this).css("background-color", "yellow");
        }, function(){
        $(this).css("background-color", "pink");
      });
    });
    

    html

    <p>Hover the mouse pointer over this paragraph.</p>
    

    in this code you have a three function in jquery first you ready a function which is the basic of function of jquery then second you have a hover function in this function when you hover a pointer to the text the color will be changed and then the next the when you release the pointer to the text it will be the different color and this is the third function

    0 讨论(0)
  • 2020-11-21 23:41
    <style>a:hover { }</style>
    <a href="/">Go Home</a>
    

    Hover is a pseudo class, and thus cannot be applied with a style attribute. It is part of the selector.

    0 讨论(0)
  • 2020-11-21 23:42

    You can get the same effect by changing your styles with JavaScript in the onMouseOver and onMouseOut parameters, although it's extremely inefficient if you need to change more than one element:

    <a href="abc.html"
       onMouseOver="this.style.color='#0F0'"
       onMouseOut="this.style.color='#00F'" >Text</a>

    Also, I can't remember for sure if this works in this context. You may have to switch it with document.getElementById('idForLink').

    0 讨论(0)
提交回复
热议问题