該賞金到期in 4天。此問題的答案有資格獲得 100聲望獎勵。 Spaceliving正在尋找規范的答案:
我需要幫助來解決這個問題
您好,我有一個wordpress 頁面,我正在嘗試制作它,以便當有人使用url上的#click_approved引數點擊我的頁面時,它會觸發帶有 data-video 屬性的鏈接以打開模態。到目前為止,我起草了這個 JS 但沒有運氣
<script>
if(document.URL.indexOf("#click_approved") >= 0) {
document.querySelector('[data-video-id="5MS_V0SpRL8"]').click();
}
</script>
也試過這個
<script>
var element = document.querySelector("a[data-video-id='5MS_V0SpRL8']");
if(document.URL.indexOf('#click_approved') >= 0) {
setTimeout(function(){element.click();}, 5000);
}
</script>
這是我試圖讓該功能發揮作用的頁面
https://www.homecarepulse.com/home-care-tv/recruitment-retention/#click_approved
任何幫助表示贊賞!
uj5u.com熱心網友回復:
您可以直接querySelector進入setTimeout回呼函式。
<script>
if(document.URL.indexOf('#click_approved') >= 0) {
setTimeout(function(){
var element = document.querySelector("a[data-video-id='5MS_V0SpRL8']");
element.click();
}, 5000);
}
</script>
或使用 a 減少等待時間 setInterval
<script>
(function() {
if (document.URL.indexOf('#click_approved') === -1) return;
let tm = setInterval(function(){
var a = document.querySelector("a[data-video-id='5MS_V0SpRL8']");
if (a) {
clearInterval(tm);
a.click();
}
}, 500);
})();
</script>
這是使用MutationObserver API 的另一種方式,它比計時器方式更有效。它確保在元素呈現后立即觸發單擊事件。
<script>
(function() {
if (document.URL.indexOf('#click_approved') === -1) return;
let observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (!mutation.addedNodes) return
for (let node of mutation.addedNodes) {
if (node.tagName == 'A' && node.getAttribute('data-video-id') == '5MS_V0SpRL8') {
node.click();
observer.disconnect();
}
}
});
});
observer.observe(document.body, {
childList: true
, subtree: true
, attributes: false
, characterData: false
});
})();
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/352317.html
