HTML ページで 10 秒など、一定の遅延が発生した後に特定のリンクをクリックする JavaScript を作成したいと考えています。
JavaScript を教えてください。Google で検索してやっと見つけたのは、特定の LINK をクリックすることですが、すぐに見つかりました。
助けてください。
ありがとう。
HTML ページで 10 秒など、一定の遅延が発生した後に特定のリンクをクリックする JavaScript を作成したいと考えています。
JavaScript を教えてください。Google で検索してやっと見つけたのは、特定の LINK をクリックすることですが、すぐに見つかりました。
助けてください。
ありがとう。
このメソッドを含めます (必要に応じて):
function fireEvent(element,event) {
if (document.createEvent) {
// dispatch for firefox + others
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true ); // event type,bubbling,cancelable
return !element.dispatchEvent(evt);
} else {
// dispatch for IE
var evt = document.createEventObject();
return element.fireEvent('on'+event,evt)
}
}
次に、それを呼び出します。
window.setTimeout(function() {
var e = document.getElementById('yourLinkId');
if(e) fireEvent(e, 'click');
}, 10000);
window.setTimeout(function() {
window.location = "http://your_url_here.com";
}, 10000);
2 番目のパラメーターはミリ秒単位の時間です。したがって、10000 ミリ秒は 10 秒です。
このサイトに示されているような遅延が必要な場合(ページの左下隅にある「証言」コンテナを参照)、次の jQuery コードを使用します。
(function($){
$(document).ready(function(){
var el = $("#testimonial"); //The id of the container. It may be a `<div>`
if (el){
RotateTestimonial();
setInterval(RotateTestimonial, 20000);
}
});
function RotateTestimonial(){
var pageUrl = "RandomTestimonial.jsp" //The page where the request goes.
$.ajax({
type: "GET",
url: pageUrl,
cache:false,
success: function(msg) {
$("#testimonial").slideUp('slow').fadeOut(3000, function (){
var el = $("#testimonial");
el.html(msg);
el.slideDown('slow').fadeIn('slow');
});
}
});
}
})(jQuery)