如何在TinyMCE中获取所选锚文本的HREF?

问题描述 投票:0回答:2

我可以通过

访问TinyMCE中选定的文本
var selection = parent.tinyMCE.activeEditor.selection.getContent();

但是我如何访问已链接的选定文本片段的 href?因此,如果文本超链接到 http://www.youtube.com 那么在选择后我可以使用 http://www.youtube.com 自动填充链接框......所以我想我是寻找类似的东西:

var href = href-of-my-selected-tinymce-text

仅供参考:我正在构建一个调用外部自定义对话框的自定义插件...

非常感谢任何可以给我提示的人:)

javascript jquery tinymce tinymce-4
2个回答
1
投票

您需要做的是将

getContent()
调用返回的字符串解析为 HTML!当您使用 jQuery 标记您的帖子时,我认为使用 jQuery 是理想的选择。话虽如此,要检索 TincyMCE 选择中的
href
元素的
a
值,请执行以下操作:

// This value of var selectionFromTinyMCE is an example
// of what parent.tinyMCE.activeEditor.selection.getContent(); returns to you
var selectionFromTinyMCE = 'sit our <a href="../forum/index.php">community forum</a>! We also';

// Here we take the string returned by TinyMCE, wrap it with a span tag,
// and pass it into a jQuery. This forces jQuery to evaluate the string as HTML!
var $jStr = $("<span>"+selectionFromTinyMCE+"</span>");

// You then create new variable and store the value of the href attribute
// of the <a> element from within your string.
var hrefValueFromTinyMCEselection = $jStr.find("a").attr("href");

// Check the console to see the result below, outputted as a string
console.log( hrefValueFromTinyMCEselection );

这是上面代码的 JSFiddle 版本,可以实时查看它的发生(打开控制台以查看记录的结果):http://jsfiddle.net/lasha/NF9V8/


0
投票

您只需要提取您需要的部分(我从选定的链接推断出 Wordpress 短代码);

selectedNode = tinyMCE.activeEditor.selection.getNode();
selected= tinyMCE.activeEditor.selection.getContent();

if( selectedNode ){. // need to test for 'http'
    //Wrap shortcode around it.
    content =  '[button href="'+selectedHref+'"]'+selected+'[/button]';

} else {
return false;
}
© www.soinside.com 2019 - 2024. All rights reserved.