How to set target attribute in tinyMCE

我只是一个虾纸丫 提交于 2019-12-22 10:36:30

问题


I have set a link to some text using tinymce.execCommand("CreateLink", False, theLink). The problem is that I want to set "Target=_blank".

How do I set a Target attribute after the above?

Thanks.


回答1:


You need to identify the correct html element in your tinymce editor. You can use this code to replace/set the target attribute of your link

$(ed.getBody()).find('a [href="'+theLink+'"]').attr('Target', '_blank');

Be aware that you might have the same link in the editor already. In this case all such links' target attributes become '_blank'.




回答2:


Are you trying to set the target attribute to _blank on all user-provided links in TinyMCE? If so, set this property in your tinymce.init configuration:

extended_valid_elements: 'a[href|target=_blank]'

This works for me on TinyMCE 4.0.2.

With this configuration, all links in the editor will have target set to _blank when the editor saves. (Additionally, all attributes other than href and target will be removed since I excluded them in the configuration; add whatever valid attributes/constraints that you wish to allow.)

see: http://www.tinymce.com/wiki.php/configuration:extended_valid_elements

If these are user-provided links, then you may also want to set rel=nofollow for all links they provide.




回答3:


Making 'New page' an only option did work for me (see: http://www.tinymce.com/wiki.php/Configuration:target_list) in version 4.1.0

target_list: [ {title: 'New page', value: '_blank', selected: true} ]



回答4:


If you prefer giving the user the option, but making target="_blank" the default.

This appears to be correct for v4.4.3:

tinymce.init({
    plugins: "link",
    default_link_target: "_blank"
});

You can also still specify the options:

tinymce.init({
    plugins: "link",
    target_list: [
        {title: 'None', value: ''},
        {title: 'Same page', value: '_self'},
        {title: 'New page', value: '_blank'}
    ],
    default_link_target: "_blank"
});

It also appears support for selected: true has been removed.




回答5:


Or you could simply:

tinymce.init({
    plugins: "link",
    target_list: [
        // remove `none` and `_self` by commenting them like below
        // {title: 'None', value: ''},
        // {title: 'Same page', value: '_self'},
        {title: 'New page', value: '_blank'}
    ]
});

http://www.tinymce.com/wiki.php/Configuration:target_list



来源:https://stackoverflow.com/questions/5513757/how-to-set-target-attribute-in-tinymce

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!