Forcing a DOM refresh in Internet explorer after javascript dom manipulation

前端 未结 6 1081
感情败类
感情败类 2020-11-27 18:07

Here is the situation. I have some javascript that looks like this:

function onSubmit() {
    doSomeStuff();
    someSpan.style.display=\"block\";
    otherS         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 18:33

    I had this problem in Chrome 21 dragging a word that had a letter with a descender ('g'). It was leaving a trail of moth dust behind on the screen, which would vanish the next time something made the screen refresh. ChrisW's solution (interrogating a layout-sensitive property) didn't work.

    What did work was to add a 1-pixel blank div at the top of the page, then remove it a millisecond later, by calling the following the function at the end of the drag operation:

    // Needed by Chrome, as of Release 21. Triggers a screen refresh, removing drag garbage.
    function cleanDisplay() {
        var c = document.createElement('div');
        c.innerHTML = 'x';
        c.style.visibility = 'hidden';
        c.style.height = '1px';
        document.body.insertBefore(c, document.body.firstChild);
        window.setTimeout(function() {document.body.removeChild(c)}, 1);
    }
    

    Note: You need the delay. Simply adding and removing the div doesn't work. Also, the div needs to be added above the part of the page that needs to be redrawn.

提交回复
热议问题