我正在使用wordpress的PDF到HTML插件,它为每个页面生成画布和文本图层。我需要在PDF中点击任何URL,所以我正在尝试编写一个将检测和URLS的脚本(使用.com作为标识符,我可能会在以后添加)。
在这个阶段我需要帮助的是将完整的URL捕获为变量,而不仅仅是包含“.com”的div
我目前有此脚本,但我需要将“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>
非常感谢任何帮助
答案 0 :(得分:1)
您可以使用此link
中的findUrls($(this).text())
$('div:contains(".com")').each(function() {
$(this).addClass('contains-url');
console.log('found div containing a URL');
var url = findUrls($(this).text())
$(this).attr('href', url);
console.log($(this).attr('href'))
});
$('.contains-url').click(function() {
console.log('clicked - this will open in a new tab');
window.open($(this).attr("href"), '_blank');
});
function findUrls(text) {
var source = (text || '').toString();
var urlArray = [];
var url;
var matchArray;
// Regular expression to find FTP, HTTP(S) and email URLs.
var regexToken = /(((ftp|https?):\/\/)[\-\w@:%_\+.~#?,&\/\/=]+)|((mailto:)?[_.\w-]+@([\w][\w\-]+\.)+[a-zA-Z]{2,3})/g;
// Iterate through any URLs in the text.
while ((matchArray = regexToken.exec(source)) !== null) {
var token = matchArray[0];
urlArray.push(token);
}
return urlArray;
}
&#13;
div {
border: 1px solid #000;
padding: 5px;
width: 100%;
margin-bottom: 10px;
}
&#13;
<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>
&#13;