2014-01-07 25 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 ... –