[英]Jquery change background color of div when mouseover
I try execute jquery for change background color of div , but all inside php code , and when put the mouse over this div should change color of background , but i think writte bad something and don´t get this effect finally 我尝试执行jquery来更改div的背景颜色,但是所有php代码都在其中,并且当将鼠标放在该div上时,应该会更改背景的颜色,但是我认为写得不好,最终没有得到这种效果
My code it´s simple and it´s this : 我的代码很简单,就是这样:
echo '<div id="rating_poll_front" onmouseover="this.css("background-color","red")" onmouseleave="this.css("background-color","yellow");" style="background-color:yellow;"></div> ';
Thank´s for the help to community , Regards !!! 谢谢大家对社区的帮助!
You should use $(this)
because this
returns an HTML element which didn't have the function css()
. 您应该使用$(this)
因为this
将返回一个不具有css()
函数的HTML元素。
Alternatively, you can use this.style.backgroundColor=yellow
. 另外,您可以使用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>
In plain JavaScript 用纯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>
尝试使用$(this)代替this
echo '<div id="rating_poll_front" onmouseover="$(this).css(\'background-color\',\'red\')" onmouseleave="$(this).css(\'background-color\',\'yellow\');" style="background-color:yellow;"></div> ';
First: 第一:
When you are using double quotes " in html use single quotes ' in its attributes. 在html中使用双引号“时 ,请在其属性中使用单引号' 。
This 这个
<div id="rating_poll_front" onmouseover="this.css("background-color","red")" onmouseleave="this.css("background-color","yellow");" style="background-color:yellow;"></div>
Should be: 应该:
<div id="rating_poll_front" onmouseover="this.css('background-color','red')" onmouseleave="this.css('background-color','yellow');" style="background-color:yellow;"></div>
Second: 第二:
As the php code is also using single quotes so use a backslash before the single quote inside php: 由于php代码也使用单引号,因此请在php中的单引号之前使用反斜杠:
Example: 例:
echo '<div id="rating_poll_front" onmouseover="this.css(\'background-color\',\'red\')" onmouseleave="this.css(\'background-color\',\'yellow\');" style="background-color:yellow;"></div>';
Third there is no such property as css() in javascript, use this.style.backgroundColor: 第三,在javascript中没有css()这样的属性,请使用this.style.backgroundColor:
So your code finally becomes: 因此,您的代码最终变为:
echo '<div id="rating_poll_front" onmouseover="this.style.backgroundColor=\'red\';" onmouseleave="this.style.backgroundColor=\'yellow\';" style="background-color:yellow;">Hello</div>';
When it comes to browser it will run like the snippet: 当涉及到浏览器时,它将像代码片段一样运行:
<div id="rating_poll_front" onmouseover="this.style.backgroundColor='red';" onmouseleave="this.style.backgroundColor='yellow';" style="background-color:yellow;">Hello</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.