关于JavaScript的问题; HTML生成和搜索当前网页的标签

问题描述:

您好我有3个问题,如果你有例如这个简单的网站关于JavaScript的问题; HTML生成和搜索当前网页的标签

<html> <head> </head> <body> <table> 
<tr> <td><a 
href="http://www.hello1.com">www.hello1.com</a></td> 
</tr> <tr> <td><a 
href="http://www.hello2.com">www.hello2.com</a></td> 
</tr> </table> </html> 

问题1)

如果我例如决定点击链接2(www.hello2.com),这是存储在某种变量?

我知道,这是存储当前的URL,但不是说你点击一个

window.location.href; 

问题2) 你如何搜索文档,说我想搜索这个网站并存储在一个javascript数组中的所有链接这样

var myArray = []; 

searchThisWebSiteForURLS()//Do this function that I don't know to write that search this htmlsite for url's 

var myArray = [ 'http://www.hello1.com', 'http://www.hello2.com'];//This is the reslt after that the function has been executed 

问题3) 我还想写OU这些链接。说,我有这样

<html> <head> </head> <body> <table> 
    <tr> <td><a 
    href="X">X</a></td> 
    </tr> <tr> <td><a 
    href="Y"></a>Y</td> 
    </tr> </table> </html> 

Where X = http://www.hello1.com 
And Y = http://www.hello2.com 

当然另一个表作为有这样的阵列中的元件应当是尽可能多的行

<html> <head> </head> <body> <table> 
    <tr> <td><a href="X">X</a></td></tr> 
    <tr> <td><a href="Y"></a>Y</td></tr> 
    <tr> <td><a href="Z">Z</a></td></tr> 
    <tr> <td><a href="A">A</a></td></tr> 
    <tr> <td><a href="B">B</a></td></tr> 
    </table> </html> 

其中Z,A,B为各元素3, 4,5排列

var myArray = [ 'http://www.hello1.com', 'http://www.hello2.com','http://www.hello3.com','http://www.hello4.com','http://www.hello5.com']; 

编辑!----------------------------------- ---------------------------------------

真的非常感谢,大家,非常感谢!我只是有关于链接一个问题,比较两个链接时,说的阵列看起来像这样

var pageLinks = ['http://www.example.at', 'http://www.example2.at', 'http://www.someothersite.at']; 

,并说,用户按下例如“http://www.example.at”链接,那么我想创建包含相似链接的表。所以我做这样的事情

function checkForSimilarLink(theLinkToCompareWith){// in this case theLinkToCompareWith = "http://www.example.at" 
    var numLinks = pageLinks.length; 

    for(var i = 0; i < numLinks; i++) { 
     //Check if numLinks[i]== theLinkToCompareWith* 
    } 
} 

那么你会如何写这个比较函数?在这种情况下,我们可以考虑

“http://www.example.at”和“http://www.example1.at”“相同”,而“http://www.someothersite.at”明显是不是再

谢谢:)

+1

你不想在JS中的数组中存储URL,这对SEO不利。有jQuery插件可以为你搜索页面。 – Jakub 2010-12-15 21:02:48

+0

事情是这不关心,这个网站将不会在网络上,它将在一个局域网上,它只会作为一个“重定向页面”,这个网页将只会被用于每次有人点击一个链接。但是,无论如何感谢:) – David 2010-12-15 21:28:46

围棋研究JQuery的!!!! XDD最适合网页开发。

在与jQuery的第一和第二个问题:

var anchors = $('a'); //returns all <a></a> elements from here you can get the url from all of theam 

与jQueryü可以写任何你想要的元素。

var table = $('<table></table>'); 
var tr = $('<tr></tr>').appendTo(table); 
var td = $('<td></td>').setText('your link here')appendTo(tr); 

。 。 。

表。appendTo(添加表的父元素);

我不明白的问题1,但这里的问题2和3的东西:

问题2:

var pageLinks = []; 
var anchors = document.getElementsByTagName('a'); 
var numAnchors = anchors.length; 
for(var i = 0; i < numAnchors; i++) { 
    pageLinks.push(anchors[i].href); 
} 
//now pageLinks holds all your URLs 

问题3:

// say pageLinks holds your desired URLs 
var pageLinks = ['http://www.example.at', 'http://www.example2.at', 'http://www.example3.at']; 

// create an empty table 
var table  = document.createElement('table'); 

// ... and it's tbody 
var tbody  = document.createElement('tbody'); 

// loop through your URLs 
var numLinks = pageLinks.length; 
for(var i = 0; i < numLinks; i++) { 

    // create new table row... 
    var tr = document.createElement('tr'); 

    // a cell... 
    var td = document.createElement('td'); 

    // and your anchor... 
    var a = document.createElement('a'); 

    // set the anchor's href 
    a.setAttribute('href', pageLinks[i]); 
    // set the anchor's text, it's also the URL in this example 
    a.innerHTML = pageLinks[i]; 

    // append the anchor to the table cell 
    td.appendChild(a); 

    // ... and that cell to the new row 
    tr.appendChild(td); 

    // ... and that row to the tbody, right? ;-) 
    tbody.appendChild(tr); 
} 

// after all rows were added to the tbody, 
// append tbody to the table 
table.appendChild(tbody); 

// and finally append this table to any existing 
// element in your document, e.g. the body: 
document.body.appendChild(table); 

// ...or add it to a div for example: 
//document.getElementById('anyDiv').appendChild(table); 

问题1: 您可以捕获onclick事件以点击链接,并在该商店期间向变量提供任何信息(尽管如此,只有当您在onclick事件中包含return false时才会相关,因为链接会将用户带到新页面并结束会话)。

问题2和问题3被Alex解答得很好。