我想在网页上获取某些链接的标题,以在表格中显示它们。页面链接变化很大,因此我不知道如何使表“动态”以正确显示链接标题。
JavaScript有可能吗?
最佳答案
假设像下面这样的html
<div id="toc"></div>
<a href="1" title="title of a1 link">a1</a> blah blah<br>
<a href="2" title="title of a2 link">a2</a> blah blah<br>
<a href="3" title="title of a3 link">a3</a> blah blah<br>
以下javascript可以满足您的要求。
var links = document.getElementsByTagName('a'); // get all links
var toc = document.getElementById('toc'); // get the (table of contents) element where the titles will be inserted
for (var i = 0 ; i < links.length; i++)
{
// for each link create a div
newTitle = document.createElement('div');
// which will hold the title of the link
newTitle.innerHTML = links[i].title;
// and then append it to the table of contents element..
toc.appendChild( newTitle );
}
关于javascript - 从页面获取链接并将其显示在表格中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2801640/