Inserting a newline into a pre tag (IE, Javascript)

前端 未结 11 1835
走了就别回头了
走了就别回头了 2020-12-10 12:34

In IE when I insert text into a

 tag the newlines are ignored:





        
11条回答
  •  無奈伤痛
    2020-12-10 13:01

    Here is a very small tweak to Edward Wilde's answer that preserves the attributes on the

     tag.

    if (elem.tagName == "PRE" && "outerHTML" in elem) {
        var outer = elem.outerHTML;
        elem.outerHTML = outer.substring(0, outer.indexOf('>') + 1) + str + "
    "; } else { elem.innerHTML = str; }

提交回复
热议问题