我定义了两个变量,一个是数组的最后一个元素,第二个是子数组的第一个元素。每个变量单独返回元素,但是当我链接/ 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.
答案 0 :(得分:3)
从您选择的lastLine
中调用其上的querySelector
进入其中的第一个.addLine
:
const addButton = lastLine.querySelector('.addLine');
如果要检索内部的所有 .addLine
,请改用querySelectorAll
:
const addButtonsInsideLastLine = lastLine.querySelectorAll('.addLine');