简体   繁体   English

如何在Tinymce Wordpress编辑器中设置父选择标签的内容

[英]How to set content of a parent selection tag in Tinymce Wordpress editor

I need to select an image in wordpress visual editor to edit its url in a shortcode dialog window. 我需要在wordpress可视编辑器中选择一个图像,以在简码对话框窗口中编辑其url。 It work fine and the img url is replaced as well: 它工作正常,并且img网址也被替换:

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

But my problem is to replace also the parent selected image <a href> attribute with the new_url value. 但是我的问题是还要用new_url值替换父选择的图像<a href>属性。

The editor.selection.getContent() function return only the img tag: editor.selection.getContent()函数仅返回img标签:

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

and not: 并不是:

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

With jQuery i could do: 使用jQuery,我可以做到:

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

but inside the editor doesn't works. 但在编辑器内部不起作用。 How can i replace the image link url using editor.selection.setContent() function? 如何使用editor.selection.setContent()函数替换图像链接URL? Thanks 谢谢

Try this code , use selection.getNode(); 试试这个代码,使用selection.getNode();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM