Reading selection with its span tags with Rangy

百般思念 提交于 2019-12-25 03:12:08

问题


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();
        }

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.

But I'm not seeing any way in Rangy to read a selection with it's span tags.

Thanks for any help.


回答1:


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;
        }
    }
}


来源:https://stackoverflow.com/questions/22506198/reading-selection-with-its-span-tags-with-rangy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!