2017-06-05 73 views
0

我需要在wordpress可視化編輯器中選擇一個圖像以在短代碼對話框窗口中編輯它的url。它做工精細和IMG網址被替換,以及:如何在Tinymce Wordpress編輯器中設置父級選擇標籤的內容

var content = editor.selection.getContent(); 
content = content.replace(original_url, new_url); 
editor.selection.setContent(content); 

但我的問題也是替換父所選圖像<a href>屬性與new_url值。

editor.selection.getContent()函數返回僅img標籤:

<img class="wp-image-1007 size-medium" src="**original_url**" width="373" height="250" /> 

,而不是:

<a href="**original_url**" target="_blank"> 
<img class="wp-image-1007 size-medium" src="**original_url**" width="373" height="250" /> 
</a> 

使用jQuery我可以這樣做:

var imgsel = editor.selection.getNode(); 
jQuery(imgsel).parent("a").attr("href", new_url); 

,但裏面的編輯器不作品。 如何使用editor.selection.setContent()函數替換圖像鏈接的URL? 謝謝

回答

0

試試這個代碼,使用selection.getNode();

var img = editor.selection.getNode(); 
img.setAttribute('src' , newURL); 
var parent = editor.dom.getParent(img,'a'); 
parent.setAttribute('href' ,newURL); 
+0

謝謝@ m.kerkeni的回答。 'parent.setAttribute('href',newURL);'返回正確的新網址,但在tinymce對話框關閉時,href不會被新的url更新。 –

+0

在關閉tinymce對話框之前,editor.getContent()的輸出是什麼? –

+0

'

' –

相關問題