我有以下從服務器生成的 HTML:
<button id="start-diag-suites" title="add new suite" tabindex="-1" ></button>
如何使用 Javascript 單擊此按鈕?
我試過了:
document.getElementById('start-diag-suites').click();
我嘗試了稍微復雜一點的:
element.dispatchEvent(new MouseEvent(eventName, {
view: window,
bubbles: true,
cancelable: true,
clientX: coordX,
clientY: coordY,
button: 0
}));
};
var theButton = document.querySelector('#start-diag-suites');
if (theButton) {
var box = theButton.getBoundingClientRect(),
coordX = box.left (box.right - box.left) / 2,
coordY = box.top (box.bottom - box.top) / 2;
simulateMouseEvent (theButton, 'mousedown', coordX, coordY);
simulateMouseEvent (theButton, 'mouseup', coordX, coordY);
simulateMouseEvent (theButton, 'click', coordX, coordY); }```
but neither seem to work at all
uj5u.com熱心網友回復:
使用 jQuery:
$('#start-diag-suites').click()
沒有 jQuery:
const button = document.getElementById('start-diag-suites');
button.dispatchEvent(new Event('click'))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/486030.html
標籤:javascript html
上一篇:給定以矩形左下角為中心的圓,計算圓的邊界矩形的寬度,使其半徑接觸右上角
下一篇:我無法獲得ajax回應
