【问题标题】:Check parent class in wordpress editor在 wordpress 编辑器中检查父类
【发布时间】:2015-07-26 15:16:34
【问题描述】:

每张图片只能在<div class="ed_fullwidth_image">标签中

但是如果你点击编辑器按钮超过 1 次 编辑器代码是这样的:

Editor Code in 3 点击编辑器自定义按钮:

<div class="ed_fullwidth_image">
<div class="ed_fullwidth_image">
<div class="ed_fullwidth_image"><img class="alignnone size-medium wp-image-318" src="http://localhost/wordpress/wp-content/uploads/2015/07/eagle-design.ir_-300x100.jpg" alt="" width="300" height="100" />
</div>
</div>
</div>

应该是这样的:

    <div class="ed_fullwidth_image">
          <img class="alignnone size-medium wp-image-318" src="http://localhost/wordpress/wp-content/uploads/2015/07/eagle-design.ir_-300x100.jpg" alt="" width="300" height="100" />
    </div>

我把完整的代码放在这里

js代码:

(function() {
tinymce.create("tinymce.plugins.afflpad", {
    init: function(ed, url) {
        ed.addCommand("afflpad_click", function(e) {
            // check if is image 
            if (ed.selection.getContent().indexOf('src=') > -1) {
                ed.selection.setContent('<div class="ed_fullwidth_image">' + ed.selection.getContent() + "</div>")
                ed.selection.setContent("\n")
            }
        });
        ed.addButton("afflpad_button", {
            title: "Affiliate Link",
            cmd: "afflpad_click",
            icon: "dfw",
            text: "تصویر تمام عرض",
        });

    },
    getInfo: function() {
        return {
            longname: "Affiliate links",
            author: "NAME",
            authorurl: "HOMEPAGE",
            infourl: "HOMEPAGE",
            version: tinymce.majorVersion + "." + tinymce.minorVersion
        };
    }
});

    tinymce.PluginManager.add("afflpad", tinymce.plugins.afflpad);
})();

php(wordpress):

function afflpad_mce_buttonhooks() {
    if(current_user_can("edit_posts") && current_user_can("edit_pages") && get_user_option("rich_editing") == "true") {
        add_filter("mce_external_plugins", "afflpad_register_tinymce_javascript");
        add_filter("mce_buttons", "afflpad_register_mce_buttons");  
    }
}

add_action("init", "afflpad_mce_buttonhooks");

function afflpad_register_tinymce_javascript($plugin_array) {
    $plugin_array["afflpad"] = (get_bloginfo('template_directory').'/js/editor-script.js');
    return $plugin_array;
}

function afflpad_register_mce_buttons($buttons) {
    array_push($buttons, "|", "afflpad_button");
    return $buttons;
}
add_filter('mce_css', 'tuts_mcekit_editor_style');
function tuts_mcekit_editor_style($url) {

    if ( !empty($url) )
        $url .= ',';


    $url .= (get_bloginfo('template_directory').'/editor-styles.css');

    return $url;
}

【问题讨论】:

    标签: javascript wordpress editor


    【解决方案1】:

    纯 JavaScript 解决方案:

    //add hasClass function to element prototype
    Element.prototype.hasClass = function(className) {
        return this.className && new RegExp("(^|\\s)" + className + "(\\s|$)").test(this.className);
    };
    
    document.body.onmouseup = function() {
       //check if selected text parent has class
       if(window.getSelection().anchorNode.parentNode.hasClass("post-text")) {
       //parent has the class, do what you want
          alert("has")
       } else {
       //false, do another thing
          alert("no")
       }
    }
    

    【讨论】:

    • tnx 它有一个错误:Uncaught TypeError: Cannot read property 'parentNode' of null
    • 此代码假定您已经选择了文本(突出显示)。您可以将此代码包装在 mouseup 事件中,以便在您选择文本后直接触发,检查上面的代码
    • 你能把你所有的 html/javascript 粘贴在这里并指出你正在处理的元素吗?
    • 帖子已被编辑! -> 按钮必须为每张图片点击一次
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 2014-04-16
    • 2012-05-02
    • 2017-06-28
    • 2014-05-20
    相关资源
    最近更新 更多