开发者

Disable element after having been clicked?

开发者 https://www.devze.com 2023-02-15 05:24 出处:网络
<img src=\"images/get_info.png\" onclick=\"do_ajax(\'get.p开发者_如何学JAVAhp?id=5\');this.src=\'images/get_info_disabled.png\';\" style=\"cursor:pointer;border:0;\">
<img src="images/get_info.png" onclick="do_ajax('get.p开发者_如何学JAVAhp?id=5');this.src='images/get_info_disabled.png';" style="cursor:pointer;border:0;">

I have a list of images like this and when user clicks on it, an AJAX request will be made and the image will be changed, but how can I make the image to be clickable only once?


Change your do_ajax function so it looks like this:

function do_ajax(url, elem) {
    var click = elem.onclick;
    elem.onclick = null;
    // Do your Ajax stuff
    elem.onclick = click;
}

and then call it like:

<img src="images/get_info.png" onclick="do_ajax('get.php?id=5', this); this.src='images/get_info_disabled.png';" style="cursor:pointer;border:0;">


< p id="id">
You can do this by retreiving the element in Javascript,
< /p>
var h = documentgetbyid("id").style.display="none";


To allow the element to be clicked only once, the easiest way I could think of is:

<img src="path/to/image.png" onclick="alert('you clicked the image'); this.removeAttribute('onclick');" />

JS Fiddle demo of concept.

Although I'd personally add the this.removeAttribute('onclick'); into your function.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号