2013-05-30 37 views
-1

禁用JavaScript的onclick事件,这是我对任何论坛的第一个问题,希望能得到一些有用的和快速回复..启用/与函数变量

这里是我的PHP代码:

<a href="<?php echo $sp['url']; ?>" id="bstd<?php echo $sp['Id']; ?>" target="_blank" onclick="clk('<?php echo $sp['Id']; ?>','<?php echo $_REQUEST['uid']; ?>')"><img src="images/add.jpj"></a> 

和下面的JavaScript函数:

<script> 
function clk(a,b){ 
......... the ajax code to use a,b as variables 
........ 
} 
</script> 

每到这个是工作的罚款... 不过。 在另一个JavaScript函数...我想启用/禁用的的

<script> 
function disb(p){ 
if(p>5){ 
     av=document.getElementById(bstd); //working fine 
     av.setAttribute("href", "#"); //working fine 
     av.style.cursor="default"; //working fine 
     av.onclick = cancel; //not working... i want to disable the onclick. 
    }else{ 
     av=document.getElementById(bstd); //working fine 
     av.setAttribute("href", ???); //i want the previous code url link automatically..as it was dynamic and coming from php. 
     av.style.cursor="pointer"; //working fine 
     av.onclick = ???; //i want the onclick function clk(with the dynamic php values). 

    } 
} 
</script> 

的onclick和HREF我知道它不容易下架..我想什么......所以这里是一个breif详细描述...

我有一个图像“a1”点击这个数我有多少次,我点击了这个图像...现在之后...如果我点击它超过5次...然后只...标记的onclick应该启用... 否则每次点击它应该禁用标签的onclick(我有另一个图像向下计数,所以我需要每次禁用它,天气启用或禁用)

我不知道它是有道理或不...但我想解决办法...

回答

1

移动你的Iduid数据 - *属性,那么您可以在一个方式,可以更改,恕不定义onclick失去他们。同样保留你的href的备份。

<a href="<?php echo $sp['url']; ?>" 
    data-href="<?php echo $sp['url']; ?>" 
    id="bstd<?php echo $sp['Id']; ?>" 
    target="_blank" 
    data-Id="<?php echo $sp['Id']; ?>" 
    data-uid="<?php echo $sp['uid']; ?>" 
    onclick="clk(this.getAttribute('data-Id'), this.getAttribute('data-uid'))" 
><img src="images/add.jpj"></a> 

然后调整的JavaScript这个新模式

function disb(p) { 
    var av = document.getElementById(bstd); // remember to use var 
    if (p > 5) { 
     av.href = "#"; // remove href 
     av.style.cursor = "default"; 
     av.onclick = function() { // prevent action 
      return false; 
     }; 
    } else { 
     av.href = av.getAttribute('data-href'); // restore href 
     av.style.cursor = "pointer"; 
     av.onclick = function() { // default action 
      return clk(
       this.getAttribute('data-Id'), 
       this.getAttribute('data-uid') 
      ); 
     }; 
    } 
} 
+0

首先,我衷心感谢各位的回复...我想它,而且它的工作原理.. – Lekhesh

+0

第一我衷心感谢你的答复......它为我工作......许多许多非常感谢......我为此挣扎,浪费了三天......我学到了一件新事物......谢谢veryyyyyyyyyyyyy多。 – Lekhesh