连接数组的两个索引并返回目标元素,而不是对象

时间:2019-06-15 07:21:50

标签: javascript

我定义了两个变量,一个是数组的最后一个元素,第二个是子数组的第一个元素。每个变量单独返回元素,但是当我链接/ join(?)它们时,它返回对象。

如何使用设置的变量来定位所需元素?

//define element
let lineNumber = document.getElementsByClassName('lineNumber');

//get last item in array
let lastLine = lineNumber[lineNumber.length - 1];

//define element
let addButton = document.getElementsByClassName('addLine');

//target specific instance in array (
addButton = addButton[0]; 

//how do I join the two?        
let lastButton = lastLine
+ addButton;

lastButton.style = "visibility: visible;";

console.log(lastLine); //this show the element      
console.log(addButton); //this shows the element
console.log(lastButton );   //this shows the object, should be the first `add button` of the last item on the `lastline` array.

1 个答案:

答案 0 :(得分:3)

从您选择的lastLine中调用其上的querySelector进入其中的第一个.addLine

const addButton = lastLine.querySelector('.addLine');

如果要检索内部的所有 .addLine,请改用querySelectorAll

const addButtonsInsideLastLine = lastLine.querySelectorAll('.addLine');