0

First of all, sorry for bad english.

Well, I have 3 paragraphs, with some links in each paragraphs, and I have to show the links by paragraph

heres my HTML & JS code:

<head>
    <script type="text/javascript">
            function pEn(){
        var nump=document.getElementsByTagName("p");
        var nEn=new Array();

        for (var i = 0; i < nump.length; i++) {
        nEn=document.getElementsByTagName("a");
        for (var j = 0; j < nEn.length; j++) {
                alert("Parrafo numero "+(i+1)+": "+nEn[j]);
        };
        };
            }
    </script>
</head>

<body>
    <p>
    <a href="http://www.google.es">First link</a>
    <a href="http://www.stackoverflow.com">Second link</a>
    </p>

    <p>
    <a href="http://www.neoteo.com">Third link</a>
    </p>

    <p>
    <a href="http://fp.edu.gva.es/">Fourth link</a>
    </p>

    <div>
    <button onclick="pEn()">Links by paragraph</button>
    </div>
</body>

What i want to get is: P1: www.fdsjkfls.com, P1: www.fjkdslfjsklo.com, P2: www.sdklf.com, P3: www.vnsdwo.com

Not this: P1: www.fdsjkfls.com, P1: www.fjkdslfjsklo.com, P1: www.sdklf.com, P1: www.vnsdwo.com, P2: www.fdsjkfls.com, P2: www.fjkdslfjsklo.com, P2: www.sdklf.com, P2: www.vnsdwo.com, ...

Any sugestions?

4

1 に答える 1

2

代わりに各段落のリンクを選択します。

for (var i = 0; i < nump.length; i++) {
    nEn=nump[i].getElementsByTagName("a"); // <-- Here use nump[i] instead of document
    for (var j = 0; j < nEn.length; j++) {
            alert("Parrafo numero "+(i+1)+": "+nEn[j]);
    };
};
于 2013-10-18T09:33:36.347 に答える