javascript - span.style.textAlign not working -
i've created function allows user align highlighted/selected text right/left/center, doesn't seem working.
function docenter() { { var selection = window.getselection().getrangeat(0); var selectedtext = selection.extractcontents(); var span = document.createelement("span"); span.style.textalign = "center"; span.appendchild(selectedtext); selection.insertnode(span); } }
i've tried separate function same thing, except highlights words instead of aligning them, , works:
function highlighter() { { var selection = window.getselection().getrangeat(0); var selectedtext = selection.extractcontents(); var span = document.createelement("span"); span.style.backgroundcolor = "yellow"; span.appendchild(selectedtext); selection.insertnode(span); } }
can tell me what's wrong first code?
span
inline element whereas block elements may have child content aligned. either use div
or p
instead of span
, or set
span.style.display = 'block';
Comments
Post a Comment