如何强制链接在新窗口中自动打开?

时间:2023-01-22 00:05:51

Normally, if I want to force a link to open in a new tab (or window) when posting to my blog, I have to either use the link GUI and select "open in new window", or, since I use the HTML view by default, after inserting a link, manually add the "target=" portion of the tag:

通常,如果我想在发布到我的博客时强制在新选项卡(或窗口)中打开链接,我必须使用链接GUI并选择“在新窗口中打开”,或者,因为我使用HTML视图默认情况下,插入链接后,手动添加标记的“target =”部分:

<a href="http://link.to/something.great" target="_blank">link text</a>

Is there a plugin or hook location I can use to automatically insert the target attribute, or am I stuck doing it manually?

是否有插件或钩子位置我可以用来自动插入目标属性,或者我是不是手动操作?

EDIT: I am looking specifically for a way to modify the link while I am creating it in the editor. If that isn't possible, then maybe a hack on the save process. But I don't want a "run-time" front-end hack, which isn't necessarily permanent.

编辑:我正在寻找一种方法来修改链接,而我在编辑器中创建它。如果那是不可能的,那么可能是对保存过程的破解。但我不想要一个“运行时”的前端黑客,这不一定是永久性的。

4 个解决方案

#1


Then there is always a plugin

然后总有一个插件

http://wordpress.org/extend/plugins/target-blank-in-posts-and-comments/

#2


One approach is to modify your functions.php file. See here for an example hook function. Another is to use jQuery.

一种方法是修改functions.php文件。请参阅此处获取示例钩子函数。另一种是使用jQuery。

#3


If you use jQuery 1.3+ you can easily do this with the following line of JavaScript:

如果您使用jQuery 1.3+,您可以使用以下JavaScript代码轻松完成此操作:

$("a:not([href^='http://your.website-url.here']").attr('target', '_blank');

Just add this to the load() event of jQuery.

只需将其添加到jQuery的load()事件中即可。

#4


You could do it using javascript fairly easily. Are you wanting to set target on all external links? Or just the ones within the post body?

你可以很容易地使用javascript做到这一点。您想要在所有外部链接上设置目标吗?或者只是帖子中的那些?

Either way, here's the jQuery code to do it:

无论哪种方式,这里是jQuery代码:

$(document).ready(function(){
    $("#postBody a").attr('target','_blank');
});

Assuming your post body is inside a div with the ID "postBody".

假设您的帖子正文在ID为“postBody”的div中。

#1


Then there is always a plugin

然后总有一个插件

http://wordpress.org/extend/plugins/target-blank-in-posts-and-comments/

#2


One approach is to modify your functions.php file. See here for an example hook function. Another is to use jQuery.

一种方法是修改functions.php文件。请参阅此处获取示例钩子函数。另一种是使用jQuery。

#3


If you use jQuery 1.3+ you can easily do this with the following line of JavaScript:

如果您使用jQuery 1.3+,您可以使用以下JavaScript代码轻松完成此操作:

$("a:not([href^='http://your.website-url.here']").attr('target', '_blank');

Just add this to the load() event of jQuery.

只需将其添加到jQuery的load()事件中即可。

#4


You could do it using javascript fairly easily. Are you wanting to set target on all external links? Or just the ones within the post body?

你可以很容易地使用javascript做到这一点。您想要在所有外部链接上设置目标吗?或者只是帖子中的那些?

Either way, here's the jQuery code to do it:

无论哪种方式,这里是jQuery代码:

$(document).ready(function(){
    $("#postBody a").attr('target','_blank');
});

Assuming your post body is inside a div with the ID "postBody".

假设您的帖子正文在ID为“postBody”的div中。