关于javascript的问题;Html生成和搜索当前网页中的标记
嗨,我有3个问题,如果你有这个简单的网站关于javascript的问题;Html生成和搜索当前网页中的标记,javascript,html,Javascript,Html,嗨,我有3个问题,如果你有这个简单的网站 <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"
<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>
问题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
<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>
var pageLinks = ['http://www.example.at', 'http://www.example2.at', 'http://www.someothersite.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*
}
}
问题3)
我想写下这些链接。说我还有一张这样的桌子
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
<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>
var pageLinks = ['http://www.example.at', 'http://www.example2.at', 'http://www.someothersite.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*
}
}
编辑--------------------------------------------------------------------------
哇,真的谢谢你们,真的谢谢!我还有一个关于链接的问题,当比较两个链接时,假设数组是这样的
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
<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>
var pageLinks = ['http://www.example.at', 'http://www.example2.at', 'http://www.someothersite.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“链接,然后我要创建包含类似链接的表。所以我做了类似的事情
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
<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>
var pageLinks = ['http://www.example.at', 'http://www.example2.at', 'http://www.someothersite.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"
var numLinks=pageLinks.length;
对于(变量i=0;i
那么如何编写这个比较函数呢?在这种情况下,我们可以考虑
"http://www.example.at“和”http://www.example1.at“相同”而“相同”http://www.someothersite.at“显然不是
再次感谢:)去学习JQuery!!!!XDD是web开发的最佳选择 对于jquery中的第一个和第二个问题:
var anchors = $('a'); //returns all <a></a> elements from here you can get the url from all of theam
var锚=$('a')//从这里返回所有元素您可以从所有AM获取url
使用jquery,你可以编写任何你想要的元素
var table = $('<table></table>');
var tr = $('<tr></tr>').appendTo(table);
var td = $('<td></td>').setText('your link here')appendTo(tr);
var表=$('');
var tr=$('').appendTo(表);
var 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
var pageLinks=[];
var archors=document.getElementsByTagName('a');
var numAnchors=锚点长度;
对于(变量i=0;i
问题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);
//假设页面链接包含所需的URL
var pageLinks=['http://www.example.at', 'http://www.example2.at', 'http://www.example3.at'];
//创建一个空表
var table=document.createElement('table');
// ... 而且是tbody
var tbody=document.createElement('tbody');
//循环浏览你的URL
var numLinks=pageLinks.length;
对于(变量i=0;i
问题1:
您可以捕获onclick事件以单击链接,并在该过程中将任何信息存储到您选择的变量中(不过,只有在onclick事件中包含return false
时,这才相关,因为该链接会将用户带到新页面并结束会话)
Alex很好地回答了问题2和3。你不想在JS中存储数组中的URL,这对SEO不好。有一些jQuery插件可以为您搜索页面。问题是,这并不重要,这个网站不会在web上,它将在LAN上,它将只作为“重定向页面”使用。这个页面只会在每次有人单击链接时使用。不过还是要谢谢你:)