使用带有函数变量的javascript启用/禁用onclick事件

时间:2013-05-30 15:14:06

标签: php javascript

这是我对任何论坛的第一个问题,希望得到一些有用和快速的回​​复..

这是我的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函数...我想启用/禁用

和href的onclick
<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>

我知道不容易站立......我想要的......所以这是一个简短的描述......

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

我不知道它有意义......但我想要解决方案......

1 个答案:

答案 0 :(得分: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')
            );
        };
    }
}