apply style to range of text with javascript in uiwebview

后端 未结 1 1734
别那么骄傲
别那么骄傲 2020-12-01 00:45

I am displaying some simple styled text as html in a UIWebView on iPhone. It is basically a series of paragraphs with the occasional strong or emphasized phrase. At runtim

1条回答
  •  借酒劲吻你
    2020-12-01 00:53

    I think you're asking a lot to get a complete solution for this, but it seemed interesting so I've implemented it. The following works in recent WebKit browsers, including Safari on iPhone running OS 3.0. It uses the non-standard but convenient intersectsNode method of Range, which exists in WebKit but was removed from Firefox in 3.0, so it doesn't work in recent versions of Firefox but could be made to do so trivially.

    The following will surround each selected text node with a element with a class of "someclass" and also a unique class to allow easy undoing. applyClassToSelection returns this unique class; pass this class into removeSpansWithClass to remove the spans.

    UPDATE: Fixed problem when selection is entirely contained within a single text node

    UPDATE 2: Now tested and works in iPhone running OS 3.0.

    UPDATE 3: Added rangeIntersectsNode function to add support for Firefox 3.0 and later. This code should now work in Firefox 1.0+, Safari 3.1+, Google Chrome, Opera 9.6+ and possibly others (untested so far). It does not work at all in Internet Explorer and will give errors in that browser. I plan to work on an IE version soon.

    
    
    
    
    

    0 讨论(0)
提交回复
热议问题