2014-05-17 79 views
0

內的任何想法,我怎麼能在邊寫這樣的代碼PHP腳本:寫javascript PHP代碼

<a href = "javascript:void(0)" onclick = "document.getElementById('lightpos<?=$the_job_id;?>').style.display='block';document.getElementById('fadepos<?=$the_job_id;?>').style.display='block'"><img src='img/remove.png' onmouseover=this.src='img/remove_light.png' onmouseout=this.src='img/remove.png'></a> 

我都試過了,這一點,但不工作:

<?php 
    echo" <a href = 'javascript:void(0)' onclick = 'document.getElementById('lightpos<?=$the_job_id;?>').style.display='block';document.getElementById('fadepos<?=$the_job_id;?>').style.display='block' '><img src='img/remove.png' onmouseover=this.src='img/remove_light.png' onmouseout=this.src='img/remove.png'></a> "; 
?> 

什麼想法?

+1

第一個將只是正常工作。如果第二個不起作用,由於引號問題,最可能發生這種情況。你可能不得不逃脫它們。你擁有的嵌套字符串越多,就越容易獲得,這就是爲什麼你應該避免這樣的事情。 –

+0

是的,我知道......這是我不知道如何修復它 – user2491321

+1

如果你看看上面的顏色編碼,它會給你一些提示? – adeneo

回答

3

答案是不迴應的HTML這麼多,爲什麼你的代碼是失敗,因爲你正在使用<?=echo聲明,也不過是echo

速記所以做這樣裏面的原因,

<a href = "javascript:void(0)" onclick = "clicked(<?=$the_job_id;?>, <?=$the_job_id;?>)"> 
    <img src='img/remove.png' onmouseover="this.src='img/remove_light.png'" onmouseout="this.src='img/remove.png'" /> 
</a> 

<script> 
    //If you are sure that lightposid and fadeposid are going to be same 
    //than 1 parameter is sufficient 
    function clicked(lightposid, fadeposid) { 
     document.getElementById('lightpos' + lightposid).style.display='block'; 
     document.getElementById('fadepos' + fadeposid).style.display='block'; 
    } 
</script> 

看在上帝份上,使用:hover僞而是採用mouseovermouseout事件......

如果你是廁所國王交換img標記的網址,而不是使用函數。

Demo(控制檯登錄將返回不確定,因爲我不具備的要素具有id lightpos1fadepos1

注:在上面的演示中,我使用1, 1作爲值<?=$the_job_id;?>。因此,他們將是你真正的工作IDS ..


如果你願意刪除img標籤,用span元素像

<a href = "javascript:void(0)" onclick = "clicked(<?=$the_job_id;?>, <?=$the_job_id;?>)"> 
    <span class="remove"></span> 
</a> 

替換現在,使用這樣的事情在你的CSS

.remove { 
    display: inline-block; 
    height: 30px; 
    width: 30px; /* Set height and width according to your requirements */ 
    background-image: url('URL_OF_THE_REMOEV_IMAGE_GOES_HERE'); 
    background-repeat: no-repeat; 
    outline: 1px solid red; /* Remove this after you set the height and width correctly */ 
    vertical-align: middle; /* Not sure but I think you will need this */ 
} 

.remove:hover { 
    background-image: url('REMOVE_LIGHT_PNG_URL_GOES_HERE'); 
} 
2

首先不使用單引號('),用雙引號(「)或無quotesfor變量。

您的代碼在單引號下有可變的$ the_job_id

試試這個代碼:

<?php 
echo '<a href ="javascript:void(0)" onclick ="clickFunc()"><img src="img/remove.png" onmouseover="Over()" onmouseout="Out()"></a>' 
?> 

,並使用此腳本:在PHP腳本

<script> 
function clickFunc() { 
    document.getElementById("lightpos'.$the_job_id.'").style.display = "block"; 
    document.getElementById("fadepos'.$the_job_id.'").style.display = "block" 
} 

function Over() { 
    this.src = "img/remove_light.png"; 
} 

function Out() { 
    this.src = "img/remove.png"; 
} 
    </script> 
+1

你的'clickFunc()'不正確,你打算爲每個錨標籤迭代該函數嗎? –