用Rangy读取带有span标签的选择 [英] Reading selection with its span tags with Rangy
问题描述
我正在使用Rangy中的CssClassApplier设置选择的字体大小:
I'm setting a font size for a selection using the CssClassApplier in Rangy:
.font16 {font-size: 16px;}
.font17 {font-size: 17px;}
.font18 {font-size: 18px;}
var font16Applier = rangy.createCssClassApplier("font16");
function applyfont16() {
font16Applier.applyToSelection();
}
现在,如果有人按下"+"来改变字体大小,我希望能够读取span标签中的类名称以供选择,例如将font16更改为font17.
Now if someone presses a "+" to bump the font size I'd like to be able to read the class name in the span tag for the selection and if it's, say, font16, change it to font17.
但是我在Rangy中看不到任何读取其span标签的选择的方法.
But I'm not seeing any way in Rangy to read a selection with it's span tags.
感谢您的帮助.
推荐答案
假定每种字体大小都有一个类应用程序,您可以依次轮流遍历每种字体并检查它是否已应用于当前选择.它效率不是很高,但可能表现不错.
Assuming you have a class applier for each font size, you could just iterate through each in turn and check if it is already applied to the current selection. It's not very efficient but may perform well enough.
var font16Applier = range.createCssClassApplier("font16");
var font17Applier = range.createCssClassApplier("font17");
var font18Applier = range.createCssClassApplier("font18");
var appliers = [font16Applier, font17Applier, font18Applier];
for (var i = 0, len = appliers.length; i < len; ++i) {
if (appliers[i].isAppliedToSelection()) {
if (i < len - 1) {
appliers[i].undoToSelection();
appliers[i + 1].applyToSelection();
break;
}
}
}
这篇关于用Rangy读取带有span标签的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!