2013-05-30 38 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