我正在尝试通过单击“按钮B”来创建一组操作
这将单击“按钮A”,这是B触发的函数的最后一个动作。
我在其他地方使用了同一行代码。我不知道为什么我在上次操作调用中总是出错。
请帮助我了解我已经研究了很多找不到答案。
function doActionsA(){
document.getElementById('view').innerHTML = "You clicked?";
}
function doActionsB(){
//Other actions befor the click action.
//I used this befor and it worked in other instances
document.getElementsByClassName('active').click();
}
<div id="view"></div>
<button class="active" onclick="doActionsA();">Button A</button>
<button class="trigger" onclick="doActionsB();"> Button B</button>
答案 0 :(得分:2)
document.getElementsByClassName返回一个元素数组。因此,您需要访问单个元素,然后执行点击操作
function doActionsA(){
document.getElementById('view').innerHTML = "You clicked?";
}
function doActionsB(){
//Other actions befor the click action.
//I used this befor and it worked in other instances
document.getElementsByClassName('active')[0].click();
}
<div id="view"></div>
<button class="active" onclick="doActionsA();">Button A</button>
<button class="trigger" onclick="doActionsB();"> Button B</button>