How to concatenate several DOM objects in an ARRAY

0

What I'm looking for is: concatenate several objects of the DOM in array dorma to make a call with a single variable that has the address of multiple children of the example tags: # menu > ul > li > a = > All this calls I seek to store it in a single variable.

Note: the code is not executed because a file called index1.php? variables is missing, I DO NOT KNOW HOW TO PLACE ANOTHER FILE

Another detail that I want to ask is this: You can print the code of a document.getElementId ('tag'); How to string?

addEventListener('load',evento,false);
// function evento() {
// 	var div 
// 	div = document.getElementById('menu');
// 	var vec = div.getElementsByTagName('li');
// 	var a = vec[].getElementsByTagName('a').addEventListener('click'url,false);
	
// 	for(i=1; i <=a.length i++){
// 		console.log(a[i]);
// 	}
function evento() {
	for (var i = 1; i <= 3; i++) {
		document.getElementById('a'+i).addEventListener('click',url,false);;
	}
}

function url(e) {
	e.preventDefault();
	var url = e.target.getAttribute('href');
	cargarUrl(url);
}

var conex;
function cargarUrl(url) {
	conex = new XMLHttpRequest();
	conex.onreadystatechange = EstadoServido;
	conex.open('GET',url,true);
	conex.send();
}

function EstadoServido() {
	var caja = document.getElementById('container')
	if (conex.readyState == 4) {
		caja.innerHTML = conex.responseText;
	}else{
		caja.innerHTML = 'Cargando .....'
	}
}
<!DOCTYPE html>
<html>
<head>
  <title>Comentarios</title>
  <script src="ajax.js"></script>
</head>
<body>
 <div id="menu">
    

    
    <li><a href="#">Enlaces creados 1</a></li>
    <li><a href="pagina1.php?fecha=12-10-2018">Enlaces creados 2</a></li>
    <li><a href="pagina1.php?fecha=13-10-2018">Enlaces creados 3</a></li>
  </div>
  <div id="container" style="border : 1px solid red;padding: 60px;width: 300px; margin: 20px;"></div>
</body>
</html>
    
asked by Gamez 11.11.2018 в 14:33
source

1 answer

0

You can use querySelectorAll to create a static list (not live) of Items that comply with the indicated selector. By static it means that it will not be updated if new elements are added that comply with the selector.

In your particular code, it might not have worked with #menu>ul>li>a because you do not have a ul element so the selector would not match anything.

Here you can see it working (after adding ul ):

var enlaces = document.querySelectorAll("#menu>ul>li>a");

console.log(enlaces);
<div id="menu">
  <ul>
    <li><a href="#">Enlaces creados 1</a></li>
    <li><a href="pagina1.php?fecha=12-10-2018">Enlaces creados 2</a></li>
    <li><a href="pagina1.php?fecha=13-10-2018">Enlaces creados 3</a></li>
  </ul>
</div>
    
answered by 12.11.2018 в 17:05