【发布时间】:2017-04-11 06:28:22
【问题描述】:
我正在为 wordpress 使用 PDF 到 HTML 插件,它为每个页面生成一个画布和一个文本层。我需要在 PDF 中使任何 URL 都可点击,因此我正在尝试编写一个脚本来检测和 URLS(使用 .com 作为标识符,稍后我可能会添加)。
在这个阶段我需要帮助的只是将完整的 URL 捕获为变量,而不仅仅是包含“.com”的 div
我目前有这个脚本,但是我需要用找到的 URL 替换“http://test.com”。
$('div:contains(".com")').each(function () {
$(this).addClass('contains-url');
console.log('found div containing a URL');
$(this).attr('href','http://test.com');
});
$('.contains-url').click(function(){
console.log('clicked - this will open in a new tab');
window.open($(this).attr("href"), '_blank');
});
div{
border: 1px solid #000; padding: 5px; width: 100%; margin-bottom: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
This div does not contain a URL
</div>
<div>
This div contains URL http://loremipsum.com
</div>
非常感谢任何帮助
【问题讨论】:
-
我还应该补充一点,上面的代码是我正在编辑的一个非常简单的版本,但是它以相同的方式工作
-
试试我的答案很简单