How do I clear inner HTML

前端 未结 4 1906
盖世英雄少女心
盖世英雄少女心 2021-02-03 23:03

I\'ve been fiddling with this for a while but it won\'t work and I can\'t figure out why. Please help. Here is what I have:



    

        
4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-03 23:33

    The problem appears to be that the global symbol clear is already in use and your function doesn't succeed in overriding it. If you change that name to something else (I used blah), it works just fine:

    Live: Version using clear which fails | Version using blah which works

    
    
        lala
    
    
        

    lalala

    This is a great illustration of the fundamental principal: Avoid global variables wherever possible. The global namespace in browsers is incredibly crowded, and when conflicts occur, you get weird bugs like this.

    A corollary to that is to not use old-style onxyz=... attributes to hook up event handlers, because they require globals. Instead, at least use code to hook things up: Live Copy

    
    
        lala
    
    
        

    lalala

    ...and even better, use DOM2's addEventListener (or attachEvent on IE8 and earlier) so you can have multiple handlers for an event on an element.

提交回复
热议问题