-1

これはフォーラムへの私の最初の質問です。有用で迅速な回答を得られることを願っています..

ここに私の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関数で...のonclickとhrefを有効/無効にしたい

<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を無効にする必要があります(下向きにカウントされる別の画像があるため、有効または無効になっているたびに無効にする必要があります)

それが理にかなっているのかどうかはわかりません...しかし、私は解決策が欲しいです...

4

1 に答える 1

1

Idanduiddata-*属性に移動すると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')
            );
        };
    }
}
于 2013-05-30T15:25:44.523 に答える