disappearing google map

前端 未结 2 1320
无人及你
无人及你 2020-12-12 03:00
> 


        
2条回答
  •  清歌不尽
    2020-12-12 03:08

    +1 Jens's answer is correct. However saying write should not normally give you document.write. window properties act as globals, but document properties do not:

    function write(){
        alert('position')
    }
    
    mybutton.onclick= function() {
        write(); // this is fine!
    };
    

    The trick is that when you write an inline event handler attribute, the properties of that element and its ancestor elements get dumped into your scope. I'm not sure this is actually documented anywhere, and certainly the exact behaviour will vary between browsers, but it's an old, well-established, and highly dangerous feature:

      // alerts A
    
    
    // alerts get

    Because document is the top ancestor of all DOM nodes in the page,

    // alerts the `document.write` function

    This means that you can't refer to any global variable or function in an inline event handler attribute that happens to have the same name as a member of an ancestor Node. And since new versions of browsers are released all the time, introducing new DOM properties, any use of any global variable or function in an event handler attribute is likely to break in the future.

    This is another reason we never use inline event handler attributes.

    [All these examples assume that alert resolves to window.alert, ie that no-one has happened to put an alert property on any of the ancestor DOM nodes...]

提交回复
热议问题