首页 > 解决方案 > 如何返回“onclick”属性?

问题描述

如果我删除一个onclick属性,我如何将它设置回与事件中应用的第一个函数相同的元素上onclick

例如function show(clicked_id)// 通过点击获取用户 ID。

我不希望用户在同一个 ID 上单击两次,所以我使用一个remove属性。然后我想把它改回原来的样子。

function getthepic(m) {
    GetPic.setAttribute("src", randomImages[m]);
    x.appendChild(GetPic);
    StorePics[i] = randomImages[m];
    StoreIds[i] = x;
    counter++;
    i++;
    CheckValve = false;

    StoreIds[0].removeAttribute("onclick");
}

setTimeout(function() {
    if (counter == 2) {
        StoreIds[0].innerHTML = "";
        StoreIds[1].innerHTML = "";
        var getCard = document.createElement('img');
        getCard.setAttribute("src", 'images/blank.png');
        StoreIds[0].appendChild(getCard);
        var getCard2 = document.createElement('img');
        getCard2.setAttribute("src", 'images/blank.png');
        StoreIds[1].appendChild(getCard2);
        counter = 0;
        i = 0;
        StorePics[0] = "";
        StorePics[1] = "";
        StoreIds[0] = "";
        StoreIds[1] = "";
        StorePics.pop();
        StoreIds.pop();
        StorePics[0].setAttribute('onclick', show(clicked_id);
    }
  
    CheckValve = true;
}, 1000);

标签: javascript

解决方案


推荐阅读