【问题标题】:Change the anchor tag in CLEditor更改 CLEditor 中的锚标记
【发布时间】:2011-07-19 19:40:12
【问题描述】:

我在我的网站 (CLEditor) 上使用 CLEditor,它运行良好,只是我希望能够将链接的目标设置为 _blank,但我无法弄清楚,即使在查看源代码时也是如此。

有没有人可以帮我把编辑器制作的链接的目标设为_blank

谢谢

【问题讨论】:

    标签: jquery wysiwyg cleditor


    【解决方案1】:

    您可以将属性添加到每个链接:

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

    【讨论】:

    • 感谢您的回答。您是否建议在提交之前运行该功能?我可以看到这个工作,但我可以想象通过改变按钮的工作方式会有一个更清洁的解决方案。但我无法完全理解源代码:$。我会暂时实现这个,谢谢。
    • 我仍然希望得到更好的答案。如果今天没有出现,我将使用(并检查)此解决方案。
    • 可能没有人愿意通过 CLE 的代码,看看如何处理 URL creaton
    • 你需要做.each吗?你就不能$("#cleeditor iframe]").contents().find("a[href]").attr('target','blank');
    • @mattcurtis 确切地说,jQuery is 非常可链接,以至于您可以只链接 attr("target", "blank") 而无需执行 each (它打破了链条并引入了新的循环功能)。
    【解决方案2】:
    (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);
    

    只需在函数 execCommand 中添加此按钮或在 cleditor 中替换:

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

    【讨论】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-04-29
        • 2023-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多