我想创建3个复选框,当选中它们时,它们会定期单击一个按钮(每个按钮)。
左键和中键是相同的情况,只是外观不同。像这样:
"transformSpec": {
"filter": {
"type": "not",
"field": {
"type": "selector",
"dimension": "name",
"value": "Ram"
}
},
"transforms": []
}
右键是这个:
<a id="fightButtonBerserk1" name="defender" value="Berserk" class="button foundation-style smallhelp profileButton fightButton" title="" href="#">
<i class="icon-friends"></i>Berserk! (5 golpes)</a>
这是将复选框插入到我想要的位置的代码,
<a id="fightButtonBerserk2" name="attacker" value="Berserk" class="button foundation-style smallhelp profileButton fightButton" title="" href="#">
<i class="icon-friends"></i>Berserk! (5 golpes)</a>
我已经有一半功能可用,但是当我要停止时,我需要一直关闭它。
我想在每个复选框中激活这种代码:
function addDomElements(){var a=$('<div class="foundation-radius fightContainer foundation-base-font"></div>'),
b=$('<input type="checkbox" class="smallerBox" id="EsquerdaHitBox" name="EsquerdaHitBox" style="margin:1px !important;line-height:10px !important;">'),
c=$('<label class="checkboxlabel" for="EsquerdaHitBox" style="margin-right: 20px;">Hit Left</label>'),
d=$('<input type="checkbox" class="smallerBox" id="MeioHitBox" name="MeioHitBox" style="margin:1px !important;line-height:10px !important;">'),
e=$('<label class="checkboxlabel" for="MeioHitBox" style="margin-right: 20px;">Hit Middle</label>'),
f=$('<input type="checkbox" class="smallerBox" id="DireitaHitkBox" name="DireitaHitkBox" style="margin:1px !important;line-height:10px !important;">'),
g=$('<label class="checkboxlabel" for="DireitaHitkBox" style="margin-right: 20px;">Hit Right</label>');
a.append(b),a.append(c),a.append(d),a.append(e),a.append(f),a.append(g),
$(a).insertAfter($('[class="foundation-radius fightContainer foundation-base-font"]')[1])}
有人可以帮我吗?
答案 0 :(得分:1)
那么,您正在尝试激活这些复选框以在选中时触发这些计时器,而在未选中时取消这些计时器吗?
如果是这样:
change
事件添加一个侦听器(以便键盘和鼠标都能工作)。data-
属性,以便代码知道哪个目标与哪个复选框一起使用。 此代码有效:
(提示:单击“运行摘要”按钮后,但在选中复选框之前,请点击该按钮右侧的“整页”链接。)
function addDomElements () {
var newNodes = $( `
<div class="mySpamCntrols foundation-radius fightContainer foundation-base-font">
<input type="checkbox" class="smallerBox" id="EsquerdaHitBox" data-trgtSelctr="#fightButtonBerserk1">
<label class="checkboxlabel" for="EsquerdaHitBox">Hit Left</label>
<input type="checkbox" class="smallerBox" id="MeioHitBox" data-trgtSelctr="#fightButtonBerserk2">
<label class="checkboxlabel" for="MeioHitBox">Hit Middle</label>
<input type="checkbox" class="smallerBox" id="DireitaHitkBox" data-trgtSelctr="#fightButtonBerserk3">
<label class="checkboxlabel" for="DireitaHitkBox">Hit Right</label>
</div>
` );
//-- Best to add a <style> node, but spam inline styles for now...
newNodes.find ("input").attr ("style", "margin:1px !important;line-height:10px !important;");
newNodes.find ("label").attr ("style", "margin-right: 20px;");
newNodes.insertAfter($('.foundation-radius.fightContainer.foundation-base-font')[1] );
//-- Activate checkboxes
$(".mySpamCntrols").on ("change", "input[data-trgtSelctr]", processMyCheckBoxes);
}
addDomElements ();
function processMyCheckBoxes (zEvent) {
var chkBox = zEvent.target;
//-- If box is now checked, fire off an immediate click and start the timers
if (chkBox.checked) {
clickButtonByjQuerySelector (chkBox.dataset.trgtselctr);
//chkBox.spamClckTmr = setInterval (clickButtonByjQuerySelector, 30000, chkBox.dataset.trgtselctr);
chkBox.spamClckTmr = setInterval (clickButtonByjQuerySelector, 2222, chkBox.dataset.trgtselctr);
chkBox.pageReloadTmr = setTimeout (location.reload.bind (window.location), 300000);
}
//-- Else, if box is now not checked, cancel the timers
else {
clearInterval (chkBox.spamClckTmr);
clearTimeout (chkBox.pageReloadTmr);
}
}
function clickButtonByjQuerySelector (jQuerySelector) {
var targetNode = $(jQuerySelector)[0];
if (targetNode) {
console.log ("Clicking node: ", jQuerySelector);
//-- Warning this my not always suffice. See https://stackoverflow.com/q/15048223
targetNode.click ();
}
else {
console.warn (`Node [${jQuerySelector}] not found!`);
}
}
/********************************************************************
******* Everything below this block, including the HTML and *******
******* CSS blocks, is simulated target page. *******
******* It's NOT part of the userscript. *******
********************************************************************/
$("button").click (zEvent => {
console.log (`Button ${zEvent.target.textContent} clicked.`);
} );
.mySpamCntrols {
margin: 1em;
border: 1px solid green;
border-radius: 0.5ex;
padding: 1ex 1em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="foundation-radius fightContainer foundation-base-font">fight 1
<button id="fightButtonBerserk1">Berserk 1</button>
<button id="fightButtonBerserk2">Berserk 2</button>
<button id="fightButtonBerserk3">Berserk 3</button>
</div>
<div class="foundation-radius fightContainer foundation-base-font">fight 2</div>