本文介绍了使用Javascript从HTML提取URL并在函数中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从html中提取URL link
.
I need to extract URL link
from html.
<a rel="nofollow" href="link" class="1">
整个页面上只有一个这样的链接.
There is only one such link on the whole page.
然后我需要在此函数中添加将其用作a.href
:
Then I need to add use it as a.href
in this function:
function changespan() {
var spans = document.querySelectorAll('span.image');
for (var i = spans.length; i--; ) {
var a = document.createElement('a');
a.href = "http://domain.com";
spans[i].appendChild(a).appendChild(a.previousSibling);
}
}
推荐答案
尝试一下:
function changespan() {
var spans = document.querySelectorAll('span.image'),
href = document.querySelector('.nofollow-link').href;
for (var i = spans.length; i--; ) {
var a = document.createElement('a');
a.href = href;
spans[i].appendChild(a).appendChild(a.previousSibling);
}
}
但是将您的链接类更改为其他内容,然后1
它不能以数字开头:
but change you link class to something else then 1
it can't start with number:
<a rel="nofollow" href="link" class="nofollow-link">Link</a>
这篇关于使用Javascript从HTML提取URL并在函数中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!