2014-01-07 26 views
0

我必须通过如何在TinyMCE中获得选定锚文本的HREF?

var selection = parent.tinyMCE.activeEditor.selection.getContent(); 

访问在TinyMCE中选定的文本,但我怎么可以访问已经被链接的文本的选择的那一段的href?因此,如果文本是超链接到http://www.youtube.com然后被选择后,我可以自动填充http://www.youtube.com链接中...所以我想我在寻找类似:

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

仅供参考:我建立该调用和外部定制对话框定制插件...

非常感谢任何人,可以给我一抬头:)

+0

你能显示一个实际的字符串,它被存储为你的'selection'变量吗?我想看看TinyMCE用'.getContents()'函数调用实际返回的结果。我可能有一个解决方案为你准备... – Lasha

+0

其实,我已经想通了。很快回答。 :) – Lasha

回答

1

你需要做的是从getContent()呼叫作为解析返回的字符串HTML!正如你用jQuery标记了你的文章,我假设为此需要使用jQuery。随着中说,你解决你的TincyMCE选择内检索a元素的href值,请执行下列操作:

// 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

工程!我不能告诉你我现在有多开心:)谢谢sooooooooooooo ... –