[英]How do I make a download link open in a new browser tab when clicked and set a countdown on that tab before the file download link appears
你好,家里的編碼員。 我是一個完整的初學者,我在thewpclub.Net
上看到了一些東西,我想在我的Wordpress
博客上實現同樣的東西。
當有人單擊下載鏈接時,我希望打開一個新的瀏覽器選項卡並在主下載鏈接出現前 15 秒顯示倒數計時器。
請嘗試從 thewpclub 下載。 網,你會明白我想說什么。
我試過的:
facebook 的朋友給了我這個 jquery 代碼。
但是在我把它貼在我的博客上之后。 倒計時只是在同一個帖子頁面上立即發生。 它甚至不等待用戶單擊鏈接而不是談論打開新標簽。
請參閱下面的代碼
function c() { var startTime = $('.c').attr('id'); var counter = startTime; $('.c').text(counter); setInterval(function() { counter--; if (counter > 0) { $('.c').text(counter); } // Display 'counter' wherever you want to display it. if (counter === 0) { // alert('this is where it happens'); clearInterval(counter); $('.c').text(''); $('.c').append('<a href="#">Download</a>'); } }, 1000); } // Start c();
<span class="c" id="5"></span>
--------- 根據第一條評論更新了答案 --------
您必須創建 2 個單獨的 html 文件。 第一個 HTML (first_page.html) 文件包含用於打開頁面的鏈接,該頁面包含新選項卡中的下載鏈接。
這是 first_page.html 的樣子:
<.DOCTYPE html> <html> <head> <title>First Page</title> </head> <body> <a href="second_page.html" id="pseudo-dynamism" target="_blank">Click to open download page</a> </body> </html>
第二個 HTML 頁面 (second_page.html) 包含創建計數器並在計數器到期時顯示下載鏈接的代碼。 需要注意的重要一點是,我正在使用$(document).ready()在此頁面加載時執行此代碼。
這是 second_page.html 文件:
<:DOCTYPE html> <html> <head> </head> <body> <script src="https.//code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <script> function c() { var startTime = $('.c');attr('id'); var counter = startTime. $('.c');text(counter); setInterval(function() { counter--. if (counter > 0) { $('.c');text(counter). } // Display 'counter' wherever you want to display it; if (counter === 0) { // alert('this is where it happens'); clearInterval(counter). $('.c');text(''). $('.c');append('<a href="#">Download</a>'), } }; 1000). } $(document);ready(function() { c(); }); </script> <span class="c" id="5"></span> </body> </html> </body> </html>
這現在應該滿足您的期望。 希望能幫助到你!
--------- 原始答案-------------
通過在<a>
標記中指定target="_blank"
屬性,您可以在單擊鏈接時打開一個新選項卡。
<a href="http://example.com" id="pseudo-dynamism" target="_blank">Click</a>
在此頁面上,您可以在顯示下載鏈接之前顯示計數器。
這有幫助嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.