【问题标题】:JS function to undo撤消的JS函数
【发布时间】:2015-02-24 09:41:01
【问题描述】:
  (function() {
tinymce.create('tinymce.plugins.custom', {
    init : function(ed, url) {
        ed.addButton('custom', {
            title : 'custom',
            text: 'custom',
            icon: false,
            onclick: function() { 
                ed.focus();
                ed.selection.setContent('<p class="custom">' + ed.selection.getContent() + '</p>');
            }
        });
    },
    createControl : function(n, cm) {
        return null;
    },
});
tinymce.PluginManager.add('custom', tinymce.plugins.custom);
})();

我已经使用上面的 JS 代码向 TinyMCE 添加了一个按钮。

因此,当用户单击按钮时,它会将所有突出显示的单词包装到标签中。

我的问题是,如果突出显示的单词已经在标签中,我该怎么做,那么它应该删除标签?

【问题讨论】:

    标签: javascript jquery wordpress onclick tinymce


    【解决方案1】:

    保留标签的数组存储,如果在数组中找到则删除标签,如果不是则推送到数组

    var tags = [];
    
    
    ...
    
    onclick: function() { 
        var content = ed.selection.getContent();
        var index = tags.indexOf(content);
        ed.focus();
    
        if (index === -1) {
            ed.selection.setContent('<p class="custom">' + content + '</p>');
            tags.push(content);
        } else {
            ed.selection.setContent(content);
            tags.splice(index, 1);
        }
    }
    

    【讨论】:

    • 如果我再次单击该按钮,我必须删除“自定义”类。现在不行了
    猜你喜欢
    • 2020-09-17
    • 2018-11-23
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 2013-07-10
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多