有人能告诉我这里的问题在哪里,我想点击#automatic以3秒延迟开始打开新窗口
$('#automatic').click(function(){
$('.autosend').each(function() {
//window.open( $(this).attr('href') );
var openwindow = window.open( $(this).attr('href') );
setTimeout(openwindow,3000);
});
});
答案 0 :(得分:1)
如果您需要以3秒的延迟打开新窗口,则此行错误:
setTimeout(openwindow,3000);
$(function () {
$('#automatic').click(function(){
$('.autosend').each(function(index, element) {
$('#txt').text($('#txt').text() + '\n' + (index * 3000));
setTimeout(function() {
var openwindow = window.open( $(this).attr('href') );
$('#txt').text($('#txt').text() + '\n' + 'Window open: ' + $(this).attr('href'));
}.bind(this), index * 3000);
});
});
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<button id="automatic">Automatic</button>
<a href="1.html" class="autosend"></a>
<a href="2.html" class="autosend"></a>
<a href="3.html" class="autosend"></a>
<a href="4.html" class="autosend"></a>
<textarea id="txt" style="height: 200px"></textarea>