谁还没个明天
2009-06-23 15:07
采纳率: 50%
浏览 1.6k

如何在内嵌 CSS 中编写: hover?

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 style attribute?

E.g. you can't reliably use CSS classes in HTML emails.

转载于:https://stackoverflow.com/questions/1033156/how-to-write-ahover-in-inline-css

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

18条回答 默认 最新

  • Didn"t forge 2009-06-23 15:09
    已采纳

    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.

    点赞 评论
  • You can't do exactly what you're describing, since a:hover is part of the selector, not the CSS rules. A stylesheet has two components:

    selector {rules}
    

    Inline styles only have rules; the selector is implicit to be the current element.

    The selector is an expressive language that describes a set of criteria to match elements in an XML-like document.

    However, you can get close, because a style set can technically go most anywhere:

    <html>
        <style>
        #uniqueid:hover {do:something;}
        </style>
        <a id="uniqueid">hello</a>
     </html>
    
    点赞 评论
  • 狐狸.fox 2009-06-23 15:10
    <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.

    点赞 评论
  • 笑故挽风 2009-06-23 15:11

    Inline pseudoclass declarations aren't supported in the current iteration of CSS (though, from what I understand, it may come in a future version).

    For now, your best bet is probably to just define a style block directly above the link you want to style:

    <style type="text/css">
        .myLinkClass:hover {text-decoration:underline;}
    </style>
    <a href="/foo" class="myLinkClass">Foo!</a>
    
    点赞 评论
  • 胖鸭 2009-06-23 15:11

    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.

    点赞 评论
  • 衫裤跑路 2009-06-23 15:13

    According to your comments, you're sending a JavaScript file anyway. Do the rollover in JavaScript. jQuery's $.hover() method makes it easy, as does every other JavaScript wrapper. It's not too hard in straight JavaScript either.

    点赞 评论
  • 妄徒之命 2009-06-23 15:16

    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').

    </div>
    
    点赞 评论
  • 斗士狗 2010-03-26 10:22

    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.

    点赞 评论
  • 游.程 2011-09-08 16:57

    I just figured out a different solution.

    My issue: I have an <a> tag around some slides/main content viewer as well as <a> tags in the footer. I want them to go to the same place in IE, so the whole paragraph would be underlined onHover, even though they're not links: the slide as a whole is a link. IE doesn't know the difference. I also have some actual links in my footer that do need the underline and color change onHover. I thought I would have to put styles inline with the footer tags to make the color change, but advice from above suggests that this is impossible.

    Solution: I gave the footer links two different classes, and my problem was solved. I was able to have the onHover color change in one class, have the slides onHover have no color change/underline, and still able to have the external HREFS in the footer and the slides at the same time!

    点赞 评论
  • python小菜 2012-03-27 05:59

    More accurate to say, you could do it at some point in the past. But now (according to the latest revision of the same standard, which is Candidate Recommendation) you can't ...

    点赞 评论
  • Lotus@ 2012-06-19 12:18

    You can use the pseudo-class a:hover in external style sheets only. Therefore I recommend using an external style sheet. The code is:

    a:hover {color:#FF00FF;}   /* Mouse-over link */
    
    点赞 评论
  • 谁还没个明天 2013-05-01 12:58

    As pointed out, you cannot set arbitrary inline styles for hover, but you can change the style of the hover cursor in CSS using the following in the appropriate tag:

    style="cursor: pointer;"
    
    点赞 评论
  • 1) Adding inline style using Javascript:

    document.head.insertAdjacentHTML('beforeend', '<style>#mydiv:hover{color:red;}</style>');
    

    or a bit harder method:

    document.getElementById("mydiv").onmouseover= function(e){this.className += ' my-special-class'; };
    document.getElementById("mydiv").onmouseleave= function(e){this.className = this.className.replace('my-special-class',''); };
    

    2) multi-word styles in Javascript:

    element.style.fontSize="12px"

    点赞 评论
  • Memor.の 2014-08-04 19:47

    You can do id by adding a class but never inline.

    <style>.hover_pointer{cursor:pointer;}</style>
    <div class="hover_pointer" style="font:bold 12pt Verdana;">Hello World</div>
    

    2 lines but you can re-use the class everywhere.

    点赞 评论
  • Didn"t forge 2015-08-10 09:41

    I'm extremely late contributing to this, however I was sad to see no one suggested this, if you actually require inline code, this is possible to do. I needed it for some hover buttons, the method is this:

    .hover-item {
        background-color: #FFF;
    }
    
    .hover-item:hover {
        background-color: inherit;
    }
    <a style="background-color: red;">
        <div class="hover-item">
            Content
        </div>
    </a

    In this case, the inline code: "background-color: red;" is the switch colour on hover, put the colour you need into there and then this solution works. I realise this may not be the perfect solution in terms of compatibility however this works if it is absolutely needed.

    </div>
    
    点赞 评论
  • 斗士狗 2016-08-29 19:05

    My problem was that I'm building a website which uses a lot of image-icons that have to be swapped by a different image on hover (e.g. blue-ish images turn red-ish on hover). I produced the following solution for this:

    .container div {
      width: 100px;
      height: 100px;
      background-size: 100px 100px;
    }
    .container:hover .withoutHover {
      display: none;
    }
    .container .withHover {
      display: none;
    }
    .container:hover .withHover {
      display: block;
    }
    <p>Hover the image to see it switch with the other. Note that I deliberately used inline CSS because I decided it was the easiest and clearest solution for my problem that uses more of these image pairs (with different URL's).
    </p>
    <div class=container>
    <div class=withHover style="background-image: url('https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQrqRsWFJ3492s0t0NmPEcpTQYTqNnH188R606cLOHm8H2pUGlH')"></div>
    <div class=withoutHover style="background-image: url('http://i.telegraph.co.uk/multimedia/archive/03523/Cat-Photo-Bombs-fa_3523609b.jpg')"></div>
    </div>

    I introduced a container containing the pair of images. The first is visible and the other is hidden (display:none). When hovering the container, the first becomes hidden (display:none) and the second shows up again (display:block).

    </div>
    
    点赞 评论
  • 游.程 2017-12-12 21:13

    This is pretty late in the game, but when would you use JavaScript in an HTML Email? For example, at the company I currently work for (rhymes with Abodee), we use the lowest common denominator for most email marketing campaigns – and JavaScript is just not being used. Ever. Unless you are referring to some kind of pre-processing.

    As mentioned in a related post: "Lotus Notes, Mozilla Thunderbird, Outlook Express, and Windows Live Mail all seem to support some sort of JavaScript execution. Nothing else does."

    Link to the article from which this was taken: [http://en.wikipedia.org/wiki/Comparison_of_e-mail_clients]

    Also, how would hovering translate to mobile devices? That's why I like the answer from above:Long answer: you shouldn't.

    If anyone has more insights into this subject, please feel free to correct me. Thank you.

    点赞 评论
  • 程序go 2018-01-13 21:31

    This is the best code example:

    <a style="color:blue;text-decoration: underline;background: white;"   href="http://aashwin.com/index.php/education/library/"    onMouseOver="this.style.color='#0F0'"
       onMouseOut="this.style.color='#00F'">Library</a>

    </div>
    
    点赞 评论

相关推荐 更多相似问题