找出元素是否已定义CSS“悬停”伪类?

时间:2018-08-30 18:26:45

标签: javascript html css pseudo-class

假设我有以下HTML:

<div id="mydiv">Hello</div>

<style>
#mydiv {
    background-color: yellow;
}

#mydiv:hover {
    background-color: red;
}
</style>

使用JavaScript,我如何确定#mydiv是否对其应用了“悬停”伪类规则?

理想情况下,它将是如下所示的功能:

function HasHoverPseudoClass(element){
    //if element has the "hover" pseudo-class then
    //return true
    //else
    //return false
}

1 个答案:

答案 0 :(得分:0)

您可以使用事件mouseover

使用JS:

var div = document.getElementById("mydiv");

div.addEventListener("mouseover", HasHoverPseudoClass);

    function HasHoverPseudoClass(){
        //if element has the "hover" pseudo-class then
        //return true
        //else
        //return false
    }

使用JQuery:

$("#mydiv").mouseover(HasHoverPseudoClass);

function HasHoverPseudoClass(){
            //if element has the "hover" pseudo-class then
            //return true
            //else
            //return false
        }

希望对您有所帮助;)