我对JavaScript很新,但对Java有很多经验,所以我开始尝试为网站编写脚本/机器人插件。但是,我为startButton创建的监听器似乎没有触发正确的onClick。
脚本正在TamperMonkey中运行(这就是为什么HTML插入在一行上,为什么===而不是==)。
我已经检查了所有id和它背后的常规逻辑,并且监听器应该工作,但它没有给出任何指示。我有它主要设置,以便其他东西将被禁用,所以我会知道startButton工作,但没有发生任何事情。当我点击stopButton时,它会提供“已经停止...”的常规警报。可以在此处找到所有相关区域的代码:http://pastebin.com/0qJ1Hvz0
我一直在环顾四周,似乎无法找到相关问题。我会在同一时间继续搜索。非常感谢你!
答案 0 :(得分:0)
试试这个......
document.getElementById('startButton').addEventListener('click', function() {
alert('do something');
});
答案 1 :(得分:0)
深入研究您的代码后:
progress
的HTML div,所以我插入一个(父级)
stopAfterXGamesCheckbox
的div实际上您的html ID是stopA F terXGamesCheckbox
stopAfterXGamesCheckbox
有一个标签,但没有带有id的div。所以我在海洋中游泳,看看会发生什么
另外,尽量不要将label for=""
作为HTML标记。
label for = stopAfterReachingXCheckbox
那样的标签;
这引起了一个错误。我认为它应该是label id = stopAfterReachingXCheckbox
最后在
下面的片段
// INSERTING UI INTO WEBPAGE
var UI_html = '<style scoped> @import url(https://fonts.googleapis.com/css?family=Ubuntu:300); table { font-family: "Ubuntu", sans-serif; background-color: #404040; color: #4A77A4; } #header { background-color: #262626; color: #ff0066; } label { font-weight: normal; } .odds { font-weight: normal; color: #00ffcc; } input[type=text] { border-radius: 5px; border: 1px solid rgba(0, 0, 0, .25); color: #808080; background-color: #262626; } input[type=text]:focus { background-color: #4A77A4; color: #ffcc00; } input[type=button] { background-color: #262626; border-color: #1a1a1a; } #startButton { color: #2ecc71; } #stopButton { color: #e74c3c; }</style><hr /><table border="1"><tbody><tr id="header"><td><input checked="checked" name="system" type="radio" value="nightengale" /> <label for="nightengaleRadioBtn">=]</label></td></tr><tr><td><table><tbody><tr><td><label for="defaultBetInput">Default Bet: </label> <input type="text" id="defaultBetInput"/></td></tr><tr><td><input type="checkbox" id="stopAfterXGamesCheckbox"/> <label >Stop after <input type="text" id="stopAfterXGamesInput"/> games</label></td></tr><tr><td><input type="checkbox" /> <label id="stopAfterReachingXCheckbox">Stop after reaching <input type="text" id="stopAfterReachingXInput"/> </label></td></tr><tr><td><label for="currentBalance">Current Balance: </label><span id="currentBalanceText"> -- </span></td></tr></tbody></table></td></tr><tr><td><input style="width: 50%;" type="button" value="Start" id="startButton"/><input style="width: 50%;" type="button" value="Stop" id="stopButton"/></td></tr></tbody></table><hr />';
var UI = document.createElement('div');
UI.innerHTML = UI_html;
var insertBefore = document.getElementsByClassName("progress")[0];
insertBefore.parentNode.insertBefore(UI, insertBefore);
// FINISHED INSERTING UI INTO WEBPAGE
// DEFINING VARIABLES
var statusBanner = document.getElementById("banner");
var redBetButton = document.getElementsByClassName("betButton")[0];
var greenBetButton = document.getElementsByClassName("betButton")[1];
var blackBetButton = document.getElementsByClassName("betButton")[2];
var balanceSpan = document.getElementById("balance_r");
var balanceSpanUI = document.getElementById("currentBalanceText");
var betAmountInput = document.getElementById("betAmount");
var defaultBetInput = document.getElementById("defaultBetInput");
var stopAfterXGamesCheckbox = document.getElementById("stopAfterXGamesCheckbox");
console.log(stopAfterXGamesCheckbox,defaultBetInput);
var stopAfterXCheckbox = document.getElementById("stopAfterReachingXCheckbox");
var stopAfterXGamesInput = document.getElementById("stopAfterXGamesInput");
var stopAfterXInput = document.getElementById("stopAfterReachingXInput");
var running = false;
// FINISHED DEFINING VARIABLES
// DEFINING LISTENERS (FOR UI)
startButton.onclick = function()
{
var passedTest = true;
if (defaultBetInput.value === '' || defaultBetInput.value <= 0)
passedTest = false;
if (stopAfterXGamesCheckbox.checked && (stopAfterXGamesInput.value === '' || stopAfterXGamesInput.value <= 0))
passedTest = false;
if (stopAfterXCheckbox.checked && (stopAfterXInput.value === '' || stopAfterXInput.value <= 0))
passedTest = false;
if (!running && passedTest)
{
running = true;
defaultBetInput.disabled = true;
stopAfterXGamesCheckbox.disabled = true;
stopAfterXGamesInput.disabled = true;
stopAfterXCheckbox.disabled = true;
stopAfterXInput.disabled = true;
// balanceSpanUI.innerHTML = getBalance();
} else if (!passedTest) {
alert("Either uncheck the boxes, or fill in the associated fields.");
} else {
alert("Already running...");
}
};
stopButton.onclick = function()
{
if (running)
{
running = false;
defaultBetInput.disabled = false;
stopAfterXGamesCheckbox.disabled = false;
stopAfterXGamesInput.disabled = false;
stopAfterXCheckbox.disabled = false;
stopAfterXInput.disabled = false;
balanceSpanUI.innerHTML = "--";
} else {
alert("Already stopped...");
}
};
defaultBetInput.oninput = function()
{
};
// FINISHED DEFINING LISTENERS (FOR UI)
// DEFINING FUNCTIONS
function getBalance()
{
return balanceSpan.innerHTML;
}
&#13;
<div class="progress">
</div>
&#13;