admin管理员组

文章数量:1418061

The problem I found is the following:

Situation: I have overall div that has a inline-block display. Inside it are two element that have an inline-block display as well.

Then I add (thanks to JavaScript) a <br/> between the two elements. The second goes to the next line, which is the normal behavior.

Buggy part: The <br/> is then removed (JavaScript again) and... the display doesn't change. It appears that the box of the overall div is not recalculated. In the end I have two similar markup that doesn't appear the same way (which is a bit problematic, isn't it).

It works fine on Firefox (it appears to be webkit based as the Android browser behave the same way). So my question is, is there a workaround that doesn't use methods that will alter the DOM? The library used is jQuery.

A test case here.

EDIT: As suggested by duri, I filled a bug report in webkit bugzilla, it's here. But I'm still looking for a workaround ;)

The problem I found is the following:

Situation: I have overall div that has a inline-block display. Inside it are two element that have an inline-block display as well.

Then I add (thanks to JavaScript) a <br/> between the two elements. The second goes to the next line, which is the normal behavior.

Buggy part: The <br/> is then removed (JavaScript again) and... the display doesn't change. It appears that the box of the overall div is not recalculated. In the end I have two similar markup that doesn't appear the same way (which is a bit problematic, isn't it).

It works fine on Firefox (it appears to be webkit based as the Android browser behave the same way). So my question is, is there a workaround that doesn't use methods that will alter the DOM? The library used is jQuery.

A test case here.

EDIT: As suggested by duri, I filled a bug report in webkit bugzilla, it's here. But I'm still looking for a workaround ;)

Share Improve this question edited Sep 2, 2011 at 11:00 Py. asked Sep 2, 2011 at 9:56 Py.Py. 3,6091 gold badge36 silver badges55 bronze badges 6
  • Interesting. Replacing the div pletely does work - jsfiddle/4yj7U/2. I'm curious to know what's the reason behind this behaviour. – pimvdb Commented Sep 2, 2011 at 10:02
  • Changing CSS style.display for div#ahah from inline-block to inline workarounds this bug for Chrome. It's important for you to have inline-block display value for div#ahah? – Andrew D. Commented Sep 2, 2011 at 10:19
  • Please consider filling a bug in Webkit's bugzilla. – duri Commented Sep 2, 2011 at 10:25
  • @Andrew: yes, it's needed for the layout I use (it's way more plex than the test case ;) ) – Py. Commented Sep 2, 2011 at 10:37
  • This is a fix that only works the first time: jsfiddle/thirtydot/4yj7U/7 – thirtydot Commented Sep 2, 2011 at 11:19
 |  Show 1 more ment

3 Answers 3

Reset to default 2

Way what I found: remove all childs from overall DIV, and then append all except BR's:

function removeBr(){
    var ahah=document.getElementById("ahah");
    var childs=[],child;
    while(child=ahah.firstChild) {
        if(!child.tagName||child.tagName.toLowerCase()!=='br')
            childs.push(child);
        ahah.removeChild(child);
    }
    for(var i=0;i<childs.length;i++)
        ahah.appendChild(childs[i]);
}

http://jsfiddle/4yj7U/4/

Other variant:

function removeBr(){
    var node=$("#ahah")[0];
    node.style.display='inline';
    $("#ahah").children("br").remove(); 
    setTimeout(function(){node.style.display='';},0);
}

As a work around, you could set the style to display: block when you want them on individual lines and revert to inline-block when you want them to be friends.

I have created a JS Fiddle example

Which demonstrates this fix:

function addBr(){
    $('span').css({ display: 'block'});
}
function removeBr(){
    $('span').css({ display: 'inline-block'});
}

$("#add").click(addBr);
$("#remove").click(removeBr);

This bug still exists, so here's another workaround: http://jsfiddle/4yj7U/19/

$("span").css('display', 'none');

setTimeout(function(){
    $('span').css('display', 'inline-block');
}, 0);

This makes Chrome re-render the spans and displays them properly.

本文标签: javascriptChromeWebkit inlineblock refresh problemStack Overflow