2010-12-15 15 views
1

您好我有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

回答

0

圍棋研究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(添加表的父元素);

2

我不明白的問題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); 
0

問題1: 您可以捕獲onclick事件以點擊鏈接,並在該商店期間向變量提供任何信息(儘管如此,只有當您在onclick事件中包含return false時纔會相關,因爲鏈接會將用戶帶到新頁面並結束會話)。

問題2和問題3被Alex解答得很好。