How to parse editable DIV's text with browser compatibility

后端 未结 3 479
孤街浪徒
孤街浪徒 2021-01-14 07:33

I make div as editable. while i tried to parse div\'s text, i was needed to do the below regular expression.

innerDOM = \"
3条回答
  •  天命终不由人
    2021-01-14 07:47

    DOM manipulation is one of the things jQuery was made for. Investing in learning jQuery will take you a long way towards writing DOM traversal and modification.

    $("div[style='cursor: text']").unwrap().prepend("
    ");

    unwrap deletes the element but keeps the children intact. The jQuery Attribute Equals Selector is used to select all divs with the cursor style attribute. You can run it live here.

    You could make this more robust as currently you would not select a div with more or less whitespace or with other trivial differences. For example:

    is not matched by the above selector. You can work around this shortcoming by introducing a CSS class that sets the cursor. In this case
    becomes
    and you can use the class selector: $("div.textCursor")

提交回复
热议问题