admin管理员组

文章数量:1406016

I am very new to web development and under major deadline. I would really appreciate your help on this.

I have an array which gets created through a javascript function which runs when user clicks on a particular table on the webpage.

<tr id="table1_1_1_0">
    <td><a href="#" onclick="func_get_fields('table1_1_1_0');">Primary</a></td>
</tr>

This function func_get_fields creates an array List_of_Screen_Names which has entries to be displayed.

My question is how do I display the elements of this returned array in the webpage so that each of them is a link in itself.

I found some code which works with the php arrays but not with javascript.

How can i do this ?

I tried another approach

document.write('<table border="1" cellspacing="1" cellpadding="5">')

for(i = 0; i < List_of_Screen_Names.length; i++){
   document.write('<tr><td>');
   document.write(List_of_Screen_Names[i]);
   document.write('</td></tr>');
}

document.write('</table>');

This creates a table of strings which I think can be changed to links. But it pletely wipes off the webpage and shows only the table. How to make it display within a div.

I am very new to web development and under major deadline. I would really appreciate your help on this.

I have an array which gets created through a javascript function which runs when user clicks on a particular table on the webpage.

<tr id="table1_1_1_0">
    <td><a href="#" onclick="func_get_fields('table1_1_1_0');">Primary</a></td>
</tr>

This function func_get_fields creates an array List_of_Screen_Names which has entries to be displayed.

My question is how do I display the elements of this returned array in the webpage so that each of them is a link in itself.

I found some code which works with the php arrays but not with javascript.

How can i do this ?

I tried another approach

document.write('<table border="1" cellspacing="1" cellpadding="5">')

for(i = 0; i < List_of_Screen_Names.length; i++){
   document.write('<tr><td>');
   document.write(List_of_Screen_Names[i]);
   document.write('</td></tr>');
}

document.write('</table>');

This creates a table of strings which I think can be changed to links. But it pletely wipes off the webpage and shows only the table. How to make it display within a div.

Share Improve this question edited Apr 8, 2014 at 7:47 John asked Apr 8, 2014 at 5:41 JohnJohn 6471 gold badge10 silver badges21 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You can modify elements inner html with javascript. For example if you want to show the results in <div id="results"></div> the code would be:

var myStringArray = ["http://google.","http://bing."]; //Sample array, use yours
var result = ""
for (var i = 0; i < myStringArray.length; i++) {
    result = result + " <a href='" + myStringArray[i] + "'>"+ myStringArray[i] + "</a>";
}
document.getElementById('results').innerHTML = result

I hope it helps!

using "split" in javascript . it's returns array value

split

function func_get_fields(d_id)
{
    var ar = d_id.split('_');
alert(ar[1]);
}

If List_of_Screen_Names is an array of links:

for(var i=0;i<List_of_Screen_Names.length;i++){
    var newLink = document.createElement('a');
    newLink.innerHTML = List_of_Screen_Names[i];
    document.body.appendChild(newLink);
}

本文标签: htmlHow to display each element of a javascript array as a linkStack Overflow