最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable onclick after click on <tr> - Stack Overflow

programmeradmin1浏览0评论

I have a lot of table rows like:

<tr class="player" onclick="document.location = \'fight.php?fightplayer='.$rowselfight['name'].'\';">

All table rows have unique links. I now want to disable all the onclick links after one of them has been clicked on. I tried editing a piece of code I found somewhere else:

$("tr").click(function() {
    $(this).attr('onclick', '');
});

But it doesn't work, does anyone have an idea how to do this?

I have a lot of table rows like:

<tr class="player" onclick="document.location = \'fight.php?fightplayer='.$rowselfight['name'].'\';">

All table rows have unique links. I now want to disable all the onclick links after one of them has been clicked on. I tried editing a piece of code I found somewhere else:

$("tr").click(function() {
    $(this).attr('onclick', '');
});

But it doesn't work, does anyone have an idea how to do this?

Share Improve this question edited Mar 25, 2014 at 22:28 Whymarrh 13.6k15 gold badges61 silver badges110 bronze badges asked Mar 25, 2014 at 22:25 s1h4d0ws1h4d0w 7816 silver badges29 bronze badges 2
  • Just wondering, if you’re already using jQuery, then why are you still using this “old-school” way of event handling via HTML attributes anyway? – C3roe Commented Mar 25, 2014 at 22:41
  • Like I said, I know nothing of jQuery or javascript, I simply looked up something that someone else posted. – s1h4d0w Commented Mar 26, 2014 at 15:44
Add a ment  | 

4 Answers 4

Reset to default 2

Try removing the onclick attribute on all the trs instead of only the one being clicked:

$("tr").click(function() {
    $("tr").attr('onclick', '');
});

Use the removeAttr function?

$("tr").click(function(){
    $(this).removeAttr("onclick");
});

This question has been asked before. You want to remove the event handler from the HTML element. Please refer to: Best way to remove an event handler in jQuery?

It sounds like you want to remove the onclick from every other element, once the original has been clicked? Or have I misunderstood the question? Try:

$("tr").click(function() {
    $("tr").attr('onclick', '');
});
发布评论

评论列表(0)

  1. 暂无评论