2012-02-22 45 views
-1

我使用該行的顏色:我想該行,以保持正確的類

echo"<tr class=\"normal\" onClick=\"this.className='normalselected'\" 
      onmouseover=\"this.className='normalon'\" 
      onmouseout=\"this.className='normal'\">\n"; 

當你移動了它的工作原理,當我移動到另一行,當你只有點擊它可以追溯到工作正常。

我該如何改變這種情況?

+2

逃脫狂歡,y!你知道你也可以在PHP/HTML中使用單引號嗎?除此之外,懸停樣式不應該使用JavaScript,而應該使用':hover' css僞類來完成。 – ThiefMaster 2012-02-22 12:32:09

+3

這是一個PHP的問題?我編輯刪除php標籤並添加了javascript ..希望沒關係.. – mishu 2012-02-22 12:33:45

+0

是的,這是OK :) – 2012-02-22 12:46:26

回答

1

這是因爲onmouseout把它恢復正常;你需要記住什麼時候點擊。雖然這不是一個「好」的解決辦法應該做你想做

什麼在你的CSS

.normal-clicked, 
.normal-over { 
    /* whatever needs to go here */ 
} 
.normal { 
    /* whatever needs to go here */ 
} 

然後在PHP

?> 
<script type="text/javascript"> 
    function handleClick(el){ 
     if (el.className == 'normal-clicked'){ 
      el.className = 'normal'; 
     } 
     else { 
      el.className = 'normal-clicked'; 
     } 
    } 
    function handleMouseOver(el){ 
     el.className = 'normal-over'; 
    } 
    function handleMouseOut(el){ 
     if (el.className == 'normal-over'){ 
      el.className = 'normal'; 
     } 
    } 
</script> 
<?php 
echo '<tr class="normal" onclick="handleClick(this);" onmouseover="handleMouseOver(this);" onmouseout="handleMouseOut(this);">', "\n"; 
1
echo "<tr class=\"normal\" onclick=\"this.className='normalselected'\" 
     onmouseover=\"if (!/normalselected/gi.test(this.className)) this.className='normalon'\" 
     onmouseout=\"if (!/normalselected/gi.test(this.className)) this.className='normal'\">\n"; 

雖然我會認真考慮使用CSS懸停和適當的事件監聽器。

1

即使我不同意你這樣做的方式,只是爲了回答你的問題:你正在刪除mouseout事件中正常選擇的類,這很正常。您可以防止僅僅通過增加像

if('normalon' == this.className) 

一個條件,你mouseout事件處理程序

還當你對一個已經選定行再來你可能想阻止添加normalon類..所以你會需要添加類似

if('normal' == this.className) 

到您的onmouseover屬性..但你真的應該考慮使用功能,而不是所有這些條件,如果你打算使用JavaScript來堅持這一

相關問題