I'm working on Rich Text Editor for web browser and I want to work with values of current font color and size in the RTE/ContentEditable element. Is there some preselected function to get these values, like execCommand, which is connected directly with ContentEditable element? Or should I use some text range jQuery library which will get these properties?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
You can use
document.queryCommandValue()
to get the colour and font size of the current selection in all major browsers:Live demo: http://jsfiddle.net/timdown/AJBsY/
Code:
However, the results are inconsistent across browsers and the
FontSize
command only works with font sizes 1-7 used in the HTML<font>
element rather than CSS, so you'd be better off getting hold of the element containing the selection and examining its CSS properties usingwindow.getComputedStyle()
/currentStyle
:Live demo: http://jsfiddle.net/timdown/K4n2j/
Code:
This is an improvement, but there are still browser inconsistencies such as differing units or colour formats.