Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何仅使用Javascript将href URL中的以下代码div(navcnt)存储到数组对象中_Javascript - Fatal编程技术网

如何仅使用Javascript将href URL中的以下代码div(navcnt)存储到数组对象中

如何仅使用Javascript将href URL中的以下代码div(navcnt)存储到数组对象中,javascript,Javascript,实际上,我的页面中有以下代码。我有(navcnt)的DIV id。我需要所有锚定标记URL只使用Javascript存储一个数组对象 我使用了var x=document.getElementById(“navcnt”)但是如何从x变量获取URL呢 <div id="navcnt"> <table id="nav" style="border-collapse:collapse;text-align:left;margin:30px auto 30px"> <tbo

实际上,我的页面中有以下代码。我有(navcnt)的DIV id。我需要所有锚定标记URL只使用Javascript存储一个数组对象

我使用了
var x=document.getElementById(“navcnt”)
但是如何从x变量获取URL呢

<div id="navcnt">
<table id="nav" style="border-collapse:collapse;text-align:left;margin:30px auto 30px">
<tbody>
<tr valign="top">
<td class="b navend">
<a id="pnprev" class="pn" style="text-decoration:none"   href="/search?q=spring+mvc+login+example&ei=ydgFU9n8HYulrQfXhoDADg&start=40&sa=N&biw=1366&bih=363">
</td>
<td>
<a class="fl" href="/search?q=spring+mvc+login+example&ei=ydgFU9n8HYulrQfXhoDADg&start=0&sa=N&biw=1366&bih=363">
<span class="csb gbil ch" style="background:url(/images/nav_logo170_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>1
</a>
</td>
<td>
<a class="fl" href="/search?q=spring+mvc+login+example&ei=ydgFU9n8HYulrQfXhoDADg&start=10&sa=N">
 <span class="csb gbil ch" style="background:url(/images/nav_logo170_hr.png)      no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>
 2
</a>
</td>
<td>
<a class="fl" href="/search?q=spring+mvc+login+example&ei=ydgFU9n8HYulrQfXhoDADg&start=20&sa=N"><span class="csb gbil ch" style="background:url(/images/nav_logo170_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>
 3</a>
</td>
<td>
<a class="fl" href="/search?q=spring+mvc+login+example&ei=ydgFU9n8HYulrQfXhoDADg&start=30&sa=N"><span class="csb gbil ch" style="background:url(/images/nav_logo170_hr.png)no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>
4
</a>
</td>
<td>
 <a class="fl" href="/search?q=spring+mvc+login+example&ei=ydgFU9n8HYulrQfXhoDADg&start=40&sa=N"><span class="csb gbil ch" style="background:url(/images/nav_logo170_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>
 5
 </a>
 </td>
 </tr>
 </tbody>
</table>


您可以使用JQuery,首先获取navcnt div:

var navcnt = $('navcnt');
例如,您可以解析前面变量中的所有锚点 并将所有URL存储在一个数组中:

var urlsList = new Array();
$('a', navcnt).each(function(){
    urlsList.push(this.attr('href'));
});

仅使用JavaScript,您应该能够按照以下方式进行操作:

var list, href, i;
var urlsList = new Array();
list = document.getElementById('navcnt').getElementsByTagName('a');
for(i=0;i<list.length;i++) {
    urlsList.push(list[i].href);
}
var列表,href,i;
var urlsList=新数组();
list=document.getElementById('navcnt').getElementsByTagName('a');

对于(i=0;i首先我要感谢您的回答。我只需要javascript逻辑谢谢您。工作正常