click() 方法无效控制台返回错误 JavaScript
click() method not working Console returning error JavaScript
我正在尝试通过单击 "Button B" 创建一组操作
将点击 "Button 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>
document.getElementsByClassName returns 一个元素数组。所以需要访问单个元素然后进行点击操作
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>
我正在尝试通过单击 "Button B" 创建一组操作
将点击 "Button 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>
document.getElementsByClassName returns 一个元素数组。所以需要访问单个元素然后进行点击操作
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>