开发者

need help with arrays and links in javascript

Hi i have a question regarding html and javascript.

Say that i click on a link on a html site say

<tr>
<td><a href="www.hello1.com">www.hello1.com</a></td>
</tr>

then I also want to see if there are any other related link on that site with a narrow name, for example

<tr>
<td><a href="www.hello2.com">www.hello2.com</a></td>
</tr>

So what I want to do is construct a javascript method that everytime you click on a link you run this method and check what link that has been pressed, and then also search the entire html site for a similar link (here its very simple, the site only consist of a table containing links, so there will not be so much to search trough).

How is this done?

I only need the method, I know how to run the method everytime you press a link :)

开发者_如何学JAVA

Thanks in advance :)

Edit With "similar" i mean something like this

'\\b'+theUrlToGoTo+'\\b'

In other words the only thing that will change is a number after the name for example

hello1 and hello2

Edit 2

Thanks to nemophrost I now know how to do the first one. I now have a second question, before Im done, thats regarding generating html code with javascript. Now say that I have an array after I have run the everyClickFunc() func that includes

var myArray = [ 'www.hello1.com', 'www.hello2.com', 'www.hello3.com'];

I would now like to generate a simple html page like this

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

</table>
</html>

And this file is to be overwritten each time I click on a link. So the links will be diffrent depending on what links i click on, on the original site.

In other words I want something like this

<html>
<head>
</head>
<body>
<table>
<tr>
<td><a href="www.testing1.com">www.testing1.com</a></td>
</tr>
<tr>
<td><a href="www.hello1.com">www.hello1.com</a></td>
</tr>
<tr>
<td><a href="www.testing2.com">www.testing2.com</a></td>
</tr>
<tr>
<td><a href="www.hello2.com">www.hello2.com</a></td>
</tr>
<tr>
<td><a href="www.hello3.com">www.hello3.com</a></td>
</tr>
<tr>
<td><a href="www.difrent1.com">www.diffrent1.com</a></td>
</tr>
<tr>
<td><a href="www.difrent2.com">www.diffrent2.com</a></td>
</tr>
</table>
</html>

To generate a new html site that contains the following information if you click on any of the above "hello" links

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

</table>
</html>

How is this easiest solved?

Again thanks you so much in advance :)


With jQuery you could do something like this:

function everyClickFunc(urlToMatch) { // pass in something like 'www.hello1.com'
    var baseURLMatch = urlToMatch.match(/^www\.(.+\D)\d*\.com$/);

    if (baseURLMatch && baseURLMatch.length > 1) {
        var matchExp = new RegExp('^www\\.' + baseURLMatch[1] + '\\d*\\.com$');

        $('a').each(function() {
            if ($(this).attr('href').match(matchExp)) {
                doSomethingBecauseYouGotAMatch(); // Call your successful match function
            }
        });
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜