Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Onclick Javascript函数不工作_Javascript_Jquery_Css_Onclick - Fatal编程技术网

Onclick Javascript函数不工作

Onclick Javascript函数不工作,javascript,jquery,css,onclick,Javascript,Jquery,Css,Onclick,我有一个图像要旋转 <img src="images/anchor.png" alt="robot image" class="robot rotate" width="100px" height="100px" /> 最后是jquery,以使其在单击时发生 $(document).ready(function () { $('img.rotate').click(function () { $('img.rotate').addClass('hover')

我有一个图像要旋转

<img src="images/anchor.png" alt="robot image" class="robot rotate" width="100px" height="100px" />
最后是jquery,以使其在单击时发生

$(document).ready(function () {
    $('img.rotate').click(function () {
        $('img.rotate').addClass('hover');
    }, function () {
        $('img.rotate').removeClass('hover');
    });
    alert("working?");
});
但是当我点击图片时,所有发生的事情就是我的图片由于css而旋转。我想要发生的是当点击图像时,它将开始旋转,而不是当我将鼠标悬停在它上面时

这是我的小提琴

改用
.toggleClass()

$('img.rotate').click(function () {
     $(this).toggleClass('hover');
});
您还需要修改悬停的CSS

改用
.toggleClass()

$('img.rotate').click(function () {
     $(this).toggleClass('hover');
});
您还需要修改悬停的CSS

  • 在css中使用
    .hover
    。您正在从JS添加类
    。hover
    ,css
    :hover
    对它不起作用
  • 尽可能使用
    $(此)
  • 使用添加/删除类

CSS:

.rotate.hover {
    -webkit-transform:rotate(-90deg);
    -moz-transform:rotate(-90deg);
    -o-transform:rotate(-90deg);
} 
$('img.rotate').click(function () {
    $(this).toggleClass('hover');
});

JS:

.rotate.hover {
    -webkit-transform:rotate(-90deg);
    -moz-transform:rotate(-90deg);
    -o-transform:rotate(-90deg);
} 
$('img.rotate').click(function () {
    $(this).toggleClass('hover');
});
  • 在css中使用
    .hover
    。您正在从JS添加类
    。hover
    ,css
    :hover
    对它不起作用
  • 尽可能使用
    $(此)
  • 使用添加/删除类

CSS:

.rotate.hover {
    -webkit-transform:rotate(-90deg);
    -moz-transform:rotate(-90deg);
    -o-transform:rotate(-90deg);
} 
$('img.rotate').click(function () {
    $(this).toggleClass('hover');
});

JS:

.rotate.hover {
    -webkit-transform:rotate(-90deg);
    -moz-transform:rotate(-90deg);
    -o-transform:rotate(-90deg);
} 
$('img.rotate').click(function () {
    $(this).toggleClass('hover');
});
试试这个:

css

.rotate{
    -webkit-transition-duration: 0.8s;
    -moz-transition-duration: 0.8s;
    -o-transition-duration: 0.8s;
    transition-duration: 0.8s;

    -webkit-transition-property: -webkit-transform;
    -moz-transition-property: -moz-transform;
    -o-transition-property: -o-transform;
    transition-property: transform;

    overflow:hidden;



}  

.rotateHover{
    -webkit-transform:rotate(-90deg);
    -moz-transform:rotate(-90deg);
    -o-transform:rotate(-90deg);
}
$(".rotate").on("click", function(){
    $(this).toggleClass("rotateHover");
});
js

.rotate{
    -webkit-transition-duration: 0.8s;
    -moz-transition-duration: 0.8s;
    -o-transition-duration: 0.8s;
    transition-duration: 0.8s;

    -webkit-transition-property: -webkit-transform;
    -moz-transition-property: -moz-transform;
    -o-transition-property: -o-transform;
    transition-property: transform;

    overflow:hidden;



}  

.rotateHover{
    -webkit-transform:rotate(-90deg);
    -moz-transform:rotate(-90deg);
    -o-transform:rotate(-90deg);
}
$(".rotate").on("click", function(){
    $(this).toggleClass("rotateHover");
});
试试这个:

css

.rotate{
    -webkit-transition-duration: 0.8s;
    -moz-transition-duration: 0.8s;
    -o-transition-duration: 0.8s;
    transition-duration: 0.8s;

    -webkit-transition-property: -webkit-transform;
    -moz-transition-property: -moz-transform;
    -o-transition-property: -o-transform;
    transition-property: transform;

    overflow:hidden;



}  

.rotateHover{
    -webkit-transform:rotate(-90deg);
    -moz-transform:rotate(-90deg);
    -o-transform:rotate(-90deg);
}
$(".rotate").on("click", function(){
    $(this).toggleClass("rotateHover");
});
js

.rotate{
    -webkit-transition-duration: 0.8s;
    -moz-transition-duration: 0.8s;
    -o-transition-duration: 0.8s;
    transition-duration: 0.8s;

    -webkit-transition-property: -webkit-transform;
    -moz-transition-property: -moz-transform;
    -o-transition-property: -o-transform;
    transition-property: transform;

    overflow:hidden;



}  

.rotateHover{
    -webkit-transform:rotate(-90deg);
    -moz-transform:rotate(-90deg);
    -o-transform:rotate(-90deg);
}
$(".rotate").on("click", function(){
    $(this).toggleClass("rotateHover");
});

您就快到了,只需做以下更改:

  • 删除伪悬停事件并更改为类

    .hover {
       -webkit-transform:rotate(-90deg);
       -moz-transform:rotate(-90deg);
       -o-transform:rotate(-90deg);
    } 
    
  • 在jquery下面使用

    $('img.rotate').click(function () {
     $('img.rotate').toggleClass('hover');
    
    });
    

  • 演示:jsfiddle.net/7B8Ya/10/

    您就快到了,只需做以下更改:

  • 删除伪悬停事件并更改为类

    .hover {
       -webkit-transform:rotate(-90deg);
       -moz-transform:rotate(-90deg);
       -o-transform:rotate(-90deg);
    } 
    
  • 在jquery下面使用

    $('img.rotate').click(function () {
     $('img.rotate').toggleClass('hover');
    
    });
    
  • 演示:jsfiddle.net/7B8Ya/10/

    小提琴:

    您的错误同时存在于.js和.css中

    首先,将CSS更改为
    。悬停
    。通过使用
    rotate:hover{foo:bar}
    ,您告诉浏览器将
    foo:bar
    应用于
    类的对象。当鼠标移动到这些对象上时,请旋转它们

    //NOT .rotate:hover  
    .hover {
        -webkit-transform:rotate(-90deg);
        -moz-transform:rotate(-90deg);
        -o-transform:rotate(-90deg);
    } 
    
    然后,在JS中,正如另一个用户指出的那样,使用.toggle删除类
    hover
    ,并将其添加到对象中

    $(document).ready(function () {
        $('img.rotate').click(function () {
                $('img.rotate').toggleClass('hover');
            }
        )
    });
    
    小提琴:

    您的错误同时存在于.js和.css中

    首先,将CSS更改为
    。悬停
    。通过使用
    rotate:hover{foo:bar}
    ,您告诉浏览器将
    foo:bar
    应用于
    类的对象。当鼠标移动到这些对象上时,请旋转它们

    //NOT .rotate:hover  
    .hover {
        -webkit-transform:rotate(-90deg);
        -moz-transform:rotate(-90deg);
        -o-transform:rotate(-90deg);
    } 
    
    然后,在JS中,正如另一个用户指出的那样,使用.toggle删除类
    hover
    ,并将其添加到对象中

    $(document).ready(function () {
        $('img.rotate').click(function () {
                $('img.rotate').toggleClass('hover');
            }
        )
    });
    

    对于一个真正蹩脚/有趣的解决方案,如果没有任何JavaScript,您可以使用CSS选择器将焦点转移到其他地方,并使其旋转图像(只要它悬停在上方)

    因此,在图像之前,使用rotate类放置一个常规的
    ,并围绕图像,使用将焦点转移到输入的
    。在CSS中,让rotate类的对象使用固定定位并移出屏幕。确保你的身体有一个隐藏的溢出

    body {
        overflow:hidden;
    }
    .rotate {
        position: fixed;
        top:-100px;
    }
    .rotate + label img {
        /* Image stuff */
    }
    .rotate:focus + label img:hover {
        /* Image hovered/clicked stuff */
    }
    
    唯一的问题是,您的用户将无法使用箭头键滚动和退格键返回页面。这是小提琴:


    对于真正的解决方案,如果您希望它只在按住鼠标按钮时发生,您可以使用
    :hover
    :active
    伪类在没有任何JavaScript的情况下旋转图像,例如:

    对于一个真正蹩脚/有趣的解决方案,没有任何JavaScript,您可以使用CSS选择器将焦点转移到其他地方,并使其旋转图像(只要它悬停在上方)

    因此,在图像之前,使用rotate类放置一个常规的
    ,并围绕图像,使用将焦点转移到输入的
    。在CSS中,让rotate类的对象使用固定定位并移出屏幕。确保你的身体有一个隐藏的溢出

    body {
        overflow:hidden;
    }
    .rotate {
        position: fixed;
        top:-100px;
    }
    .rotate + label img {
        /* Image stuff */
    }
    .rotate:focus + label img:hover {
        /* Image hovered/clicked stuff */
    }
    
    唯一的问题是,您的用户将无法使用箭头键滚动和退格键返回页面。这是小提琴:


    对于真正的解决方案,如果您希望只在按住鼠标按钮时发生,则可以使用
    :hover
    :active
    伪类在不使用任何JavaScript的情况下旋转图像,例如:

    您的JSFIDLE实际使用
    .hover()
    。如果您不希望它在hover上执行此操作,我建议避免使用该方法。您不需要为
    .hover
    类设置任何CSS规则。您是一个类hover。
    。click()
    需要一个回调函数,而不是两个。此外,在回调中,您可以使用
    $(this)
    而不是再次为节点重新转换DOM…您的JSFIDLE实际上使用
    .hover()
    。如果您不希望它在hover上执行此操作,我建议避免使用该方法。您不需要为
    .hover
    类设置任何CSS规则。您是一个类hover。
    。click()
    需要一个回调函数,而不是两个。此外,在回调中,您可以使用
    $(this)
    ,而不是再次为节点重新转换DOM。。。实际上是从jQuery中删除的。括号内的.addClass(text).removeClass(text)是您正在添加或删除的css类,如rotate:hover,而不是.hover。机器人将是识别这一信息的最好方式。机器人将是识别这一信息的最好方式,因为你正在对一个img的类的类的类的类的类的旋转进行改动。机器人将是识别这一信息的最好方式。机器人将是识别这一信息的最好方式。因为你正在对一个img的类的类的类的类的类的类的类的类的类的旋转进行改变。这是你正在做改变。机器人将是你正在做的这一次改动,机器人将是对这一个img的一类的一次次次次的一次次次次次。机器人将是识别这一次的最好的最好的最好的方式。机器人将是。机器人将是。机器人将是。机器人将是从从从从从你将是识别这将是识别这一次的最好的最好的最好的最好的最好的最好的方式。机器人将是这一次。机器人将是一次。机器人将是一次。机器人将是从从从从你将是对这一次次次次uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu我使用了切换开关,因为它的工作原理和鼠标的悬停类似,除了你们点击鼠标外,我使用了和鼠标的工作方式和鼠标的工作方式与鼠标的悬停相似的我使用了我使用过的切换切换,除了你们的工作方式与鼠标的工程工程工程类似,除了你们点击外,除了你们外,除了你们点击。除了你们点击。除了你们外,除了你们点击鼠标鼠标外,除了你们外,除了你们的鼠标鼠标点击。厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄普普普普普普普UUUUUUUUUU\UUUUUUUUUUUUUUU\ uuuuuuuUUUUUUUUUUUUU厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄厄uuuuuuuuuuuuuuuuuuuuu好处:通过添加过渡css使过渡看起来平滑。实际上是从jQuery中删除的