gpt4 book ai didi

javascript - Jquery改变鼠标悬停时div的背景颜色

转载 作者:行者123 更新时间:2023-11-28 14:56:42 26 4
gpt4 key购买 nike

我尝试执行jquery来更改div的背景颜色,但全部在php代码内,当将鼠标放在这个div上时应该改变背景颜色,但我认为写错了一些东西,最终没有得到这个效果

我的代码很简单,就是这样:

   echo '<div id="rating_poll_front" onmouseover="this.css("background-color","red")" onmouseleave="this.css("background-color","yellow");" style="background-color:yellow;"></div> ';

感谢您对社区的帮助,此致!!!

最佳答案

您应该使用 $(this),因为 this 返回的 HTML 元素没有 css() 函数。
或者,您可以使用 this.style.backgroundColor=yellow

Please escape or use single quote inside a set of double quote

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="rating_poll_front" onmouseover="$(this).css('background-color','red')" onmouseleave="$(this).css('background-color','yellow');" style="background-color:yellow;">dsfdsf</div>

用纯 JavaScript 编写

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="rating_poll_front" onmouseover="this.style.backgroundColor='red';" onmouseleave="this.style.backgroundColor='yellow';" style="background-color:yellow;">dsfdsf</div>

关于javascript - Jquery改变鼠标悬停时div的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42599316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com