第一:
當您使用雙引號「在HTML中使用單引號」在其屬性。
這
<div id="rating_poll_front" onmouseover="this.css("background-color","red")" onmouseleave="this.css("background-color","yellow");" style="background-color:yellow;"></div>
應該是:
<div id="rating_poll_front" onmouseover="this.css('background-color','red')" onmouseleave="this.css('background-color','yellow');" style="background-color:yellow;"></div>
二:
由於PHP代碼也使用單引號內的PHP的單引號之前,所以用一個反斜槓:
例如:
echo '<div id="rating_poll_front" onmouseover="this.css(\'background-color\',\'red\')" onmouseleave="this.css(\'background-color\',\'yellow\');" style="background-color:yellow;"></div>';
第三,在javascript中沒有像css()那樣的屬性,請使用this.style。的backgroundColor:
所以,你的代碼最終變爲:
echo '<div id="rating_poll_front" onmouseover="this.style.backgroundColor=\'red\';" onmouseleave="this.style.backgroundColor=\'yellow\';" style="background-color:yellow;">Hello</div>';
當談到瀏覽器將運行像片段:
<div id="rating_poll_front" onmouseover="this.style.backgroundColor='red';" onmouseleave="this.style.backgroundColor='yellow';" style="background-color:yellow;">Hello</div>
'this'給HTMLElement對象沒有'.css()'。改用'$(this)'。 –
,因爲'this'不是一個jQuery對象 –