2014-05-17 81 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()'不正确,你打算为每个锚标签迭代该函数吗? –