jquery - 来自文本的打开链接变成可点击的链接
问题描述
我正在尝试使用 jQuery 自动打开一个链接(最初是纯文本)。
在 html 代码中,链接是这样编写的:
<p><strong>Page URL:</strong><br />
https://www.google.com/</p>
我使用此代码将文本转换为可点击的链接:
(function($) {
$(document).ready(function() {
$('p').eq(7).html($('p').eq(7).html().replace(/(https?:\/\/.+?)(?:\s|$)/ig, '<a href="$1">$1</a> '));
});
})(jQuery);
我试图调整一些我在已经可点击的链接上成功使用的其他代码,但我就是想不通:
this.$ = this.jQuery = jQuery.noConflict(true);
var baseUrl = "http://www.google.com/";
var matchingLinks = $( "a[href*='" + baseUrl + "']" );
$(matchingLinks).each(
function(index)
{
window.open( $(this).attr( "href" ) );
}
);
谢谢!
解决方案
你不需要 JQuery。这段代码可以帮助你。
function htmlchars(string) {return string.replace(/[\x00-\x1f\xff-\uffff]/g,function(v) {return "&#" + v.charCodeAt() + ";"})};
document.documentElement.replace(/https?:\/\/.+?(?:\s|$)/gi,function(v){return "<a href=\"" + htmlchars(v) + "\">" + htmlchars(v) + "</a>"});
我不知道它正在工作。我无法尝试。
推荐阅读
- javascript - 我得到了一个带有 JS、HTML 和 CSS3 的代码片段。如何将它插入到 wordpress html-widget?
- r - 将 Excel 数字日期转换为 R 日期,但在某些行中只有给出的年份,例如“2018”而不是“43465”
- multithreading - 用mkldnn编译mxnet CPP包,支持单线程执行,无需环境变量
- javascript - 如何多查找聚合
- jenkins - 我们可以在用 Groovy 编写的 Jenkins 声明性管道的并行块中使用输入步骤吗?
- c++ - 有没有办法以clang格式禁用“SpacesInBraces”?
- .net - 通过命令管道后控制台重定向的问题
- c++ - 我可以为 nVIDIA GPU 编写 OpenCL-C++ 内核吗?
- autodesk-forge - CAD 文件的大小是否有任何限制,比如 Forge Viewer 可以显示的 RVT 文件?
- html - 如何修复 HTML 网页底部的空白?