Auto_click_Script_with_ID_changing 使用(document.getElementById)

Auto_click_Script_with_ID_changing using(document.getElementById)

您好,我正在尝试 运行 自动点击功能

setTimeout(function(){document.getElementById("XXXXXXXXX").click();}, 1000);

但我的 ID 随随机数而变化,第一部分是相同的

Task_23587534 Task_93783638 Task_83937637

并希望在每个页面上运行此功能。那么,如果我的最后 8 位数字总是变化,我该怎么办。

我会设置一个 onClick 事件,使您能够在页面之间具有相同的功能......而不是将 id 设置为 Task_1010101010,而是将 id 传递到函数中 Like所以:

<div id="whatever" onClick="MyFunction('10101010');"> 

然后有一个 JavaScript 函数来处理唯一标识符

function MyFunction(id){
    // Do Stuff with the variable id which will be set to 10101010 in this case....
    alert(id);
}

如果你想要一个 纯粹 jQuery 的处理方式——你可以使用 data 属性 IE

<div class="Task" data_id="10101010'">

那么 jQuery 可能看起来像:

$(".Task").on('click', function() {
   alert($(this).attr("#data-id"));
}); 

当然,这些只是将 id 放入函数中的示例..但是..设置超时等在那时应该是小菜一碟。