Home  >  Article  >  Web Front-end  >  Solution to the problem of invalidity caused by dynamically changing the onclick attribute in jquery_jquery

Solution to the problem of invalidity caused by dynamically changing the onclick attribute in jquery_jquery

WBOY
WBOYOriginal
2016-05-16 17:10:431187browse
Copy code The code is as follows:

  • < /li>

  • Although writing this way is not recommended in many cases, there are still times when you will encounter such code.

    Then you may need to modify the onclick attribute at some point as follows:
    Copy the code The code is as follows:

    $("#id").attr("onclick",url);

    Code like this can get what you want in chrome and firefox result.

    Unfortunately, in IE, at least IE7 and below have no effect. As for i8, it seems to have no effect.

    The way is to use jquery binding.
    Copy the code The code is as follows:

    $("#id").attr("onclick","").click(function(){
    //
    });

    You need to clear onclick here first The value of the attribute is displayed after binding.

    If the onclick attribute has not been set, there is no need to clear it.
    Copy the code The code is as follows:

    $("#id").click(funciton(){
    //
    });
    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn