Change the anchor tag in CLEditor

时光毁灭记忆、已成空白 提交于 2019-12-10 15:54:53

问题


Im using CLEditor on my website (CLEditor) and it is working well only I want to be able to set the target of links to _blank but I can't figure it out, even when looking into the source.

Is there someone who can help me make the links made by the editor have a target of _blank

Thanks


回答1:


You could add the attribute to each link:

$("#cleeditor iframe").contents().find("a[href]").attr("target", "_blank");



回答2:


$('textarea').cleditor({
  updateTextArea: function(html) {
    var e = $('<div>').append(html);
    e.find('a[href]').attr('target', '_blank');
    return e.html();
  }
}); 



回答3:


(function($) {

// Define the lien button
$.cleditor.buttons.lien = {
    name: "lien",
    image: "lien.gif",
    title: "Add link",
    command: "inserthtml",
    popupName: "Lien",
    popupClass: "cleditorPrompt",
    popupContent: 'Enter URL:<br><input type=text value="http://" size=35><br><input type=button value="submit">',
    buttonClick: lienClick
};

// Add the button to the default controls before the bold button
$.cleditor.defaultOptions.controls = $.cleditor.defaultOptions.controls
.replace("link", "lien link");

// Handle the lien button click event
function lienClick(e, data) {

    // Get the editor
    var editor = data.editor;

    if (editor.selectedText() === "") {
        editor.showMessage("A selection is required when inserting a link.");             
        return false;
    }

    // Wire up the submit button click event
    $(data.popup).children(":button")
    .unbind("click")
    .bind("click", function(e) {

        // Get the entered name
        var url = $(data.popup).find(":text").val();     
        var value = '<a href="' + url + '" target="_blank">' + editor.selectedText() + '</a>'
        var success = editor.doc.execCommand("insertHTML", 0, value || null)

        if (!success){
            editor.showMessage("Error executing the insertHTML command.");
        }
        // Hide the popup and set focus back to the editor
        editor.hidePopups();
        editor.focus();

    });      
}     
})(jQuery);

Just add this button or in cleditor replace in function execCommand :

try { 
      if(command.toLowerCase() == "createlink"){
        value = '<a href="' + value + '" target="_blank">' + getRange(editor) + '</a>'
        success = editor.doc.execCommand("insertHTML", 0, value || null);
      }else{
         success = editor.doc.execCommand(command, 0, value || null); 
      }
  }


来源:https://stackoverflow.com/questions/5380393/change-the-anchor-tag-in-cleditor

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