Inline style to act as :hover in CSS

前端 未结 7 2199
梦毁少年i
梦毁少年i 2020-12-01 02:11

I know that embedding CSS styles directly into the HTML tags they affect defeats much of the purpose of CSS, but sometimes it\'s useful for debugging purposes, as in:

<
相关标签:
7条回答
  • 2020-12-01 02:59

    I'm afraid it can't be done, the pseudo-class selectors can't be set in-line, you'll have to do it on the page or on a stylesheet.

    I should mention that technically you should be able to do it according to the CSS spec, but most browsers don't support it

    Edit: I just did a quick test with this:

    <a href="test.html" style="{color: blue; background: white} 
                :visited {color: green}
                :hover {background: yellow}
                :visited:hover {color: purple}">Test</a>
    

    And it doesn't work in IE7, IE8 beta 2, Firefox or Chrome. Can anyone else test in any other browsers?

    0 讨论(0)
  • 2020-12-01 03:00

    If that <p> tag is created from JavaScript, then you do have another option: use JSS to programmatically insert stylesheets into the document head. It does support '&:hover'. https://cssinjs.org/

    0 讨论(0)
  • 2020-12-01 03:05

    A simple solution:

       <a href="#" onmouseover="this.style.color='orange';" onmouseout="this.style.color='';">My Link</a>
    

    Or

    <script>
     /** Change the style **/
     function overStyle(object){
        object.style.color = 'orange';
        // Change some other properties ...
     }
    
     /** Restores the style **/
     function outStyle(object){
        object.style.color = 'orange';
        // Restore the rest ...
     }
    </script>
    
    <a href="#" onmouseover="overStyle(this)" onmouseout="outStyle(this)">My Link</a>
    
    0 讨论(0)
  • 2020-12-01 03:06

    If you are only debugging, you might use javascript to modify the css:

    <a onmouseover="this.style.textDecoration='underline';" 
        onmouseout="this.style.textDecoration='none';">bar</a>
    
    0 讨论(0)
  • 2020-12-01 03:12

    I put together a quick solution for anyone wanting to create hover popups without CSS using the onmouseover and onmouseout behaviors.

    http://jsfiddle.net/Lk9w1mkv/

    <div style="position:relative;width:100px;background:#ddffdd;overflow:hidden;" onmouseover="this.style.overflow='';" onmouseout="this.style.overflow='hidden';">first hover<div style="width:100px;position:absolute;top:5px;left:110px;background:white;border:1px solid gray;">stuff inside</div></div>
    
    0 讨论(0)
  • 2020-12-01 03:14

    If it's for debugging, just add a css class for hovering (since elements can have more than one class):

    a.hovertest:hover
    {
    text-decoration:underline;
    }
    
    <a href="http://example.com" class="foo bar hovertest">blah</a>
    
    0 讨论(0)
提交回复
热议问题