如何在 jquery 上的 set Interval 函数中添加 ''this''
how to add ''this'' inside the set Interval function on jquery
我有一个 setInterval 函数,例如 this.Appear 结果仅在第一个 window.how 中包含 setIntervel 中的 'this' 函数。帮助我解决我的问题。
$(document).ready(function(){
$("a").click(function(){
var user ='<div><b id="mainbox" name="mainbox"><form id="fm"><p id="chat-msgs"></p><input type="text" id="text" placeholder="Type msghere..."></form></b></div>'
$("#demo1").append(user);
});
});
$(document).on('click', '#mainbox', function(){
$.ajaxSetup({cache:false});
setInterval(function(){
$('#chat-msgs').load('real-auto.php');//its appear only on first window .
}, 900);
});
<a>ram</a>
<a>ravi</a>
<a>ragul</a>
<p id='demo1'></p>
你可以这样写
$(document).on('click', '#mainbox', function(){
var that = this; // store the current #mainbox
$.ajaxSetup({cache:false});
setInterval(function(){
$(that).find('#chat-msgs').load('real-auto.php');//its appear only on first window .
}, 900);
});
但是,您在这里重复添加了一些带有 id
的元素,您不能在一个网页中拥有多个具有相同 ID 的元素。
所以将所有 id
替换为 class
.
html
<a href="#">ram</a>
<a href="#">ravi</a>
<a href="#">ragul</a>
<p id='demo1'></p>
js
$(document).ready(function(){
$("a").click(function(){
var user ='<div><b class="mainbox" name="mainbox"><form class="fm"><p class="chat-msgs"></p><input type="text" class="text" placeholder="Type msghere..."></form></b></div>';
$("#demo1").append(user);
});
});
$(document).on('click', '.mainbox', function(){
var that = this; // store current .mainbox
$.ajaxSetup({cache:false});
setInterval(function(){
$(that).find('.chat-msgs').load('real-auto.php');//its appear only on first window .
}, 900);
});
我有一个 setInterval 函数,例如 this.Appear 结果仅在第一个 window.how 中包含 setIntervel 中的 'this' 函数。帮助我解决我的问题。
$(document).ready(function(){
$("a").click(function(){
var user ='<div><b id="mainbox" name="mainbox"><form id="fm"><p id="chat-msgs"></p><input type="text" id="text" placeholder="Type msghere..."></form></b></div>'
$("#demo1").append(user);
});
});
$(document).on('click', '#mainbox', function(){
$.ajaxSetup({cache:false});
setInterval(function(){
$('#chat-msgs').load('real-auto.php');//its appear only on first window .
}, 900);
});
<a>ram</a>
<a>ravi</a>
<a>ragul</a>
<p id='demo1'></p>
你可以这样写
$(document).on('click', '#mainbox', function(){
var that = this; // store the current #mainbox
$.ajaxSetup({cache:false});
setInterval(function(){
$(that).find('#chat-msgs').load('real-auto.php');//its appear only on first window .
}, 900);
});
但是,您在这里重复添加了一些带有 id
的元素,您不能在一个网页中拥有多个具有相同 ID 的元素。
所以将所有 id
替换为 class
.
html
<a href="#">ram</a>
<a href="#">ravi</a>
<a href="#">ragul</a>
<p id='demo1'></p>
js
$(document).ready(function(){
$("a").click(function(){
var user ='<div><b class="mainbox" name="mainbox"><form class="fm"><p class="chat-msgs"></p><input type="text" class="text" placeholder="Type msghere..."></form></b></div>';
$("#demo1").append(user);
});
});
$(document).on('click', '.mainbox', function(){
var that = this; // store current .mainbox
$.ajaxSetup({cache:false});
setInterval(function(){
$(that).find('.chat-msgs').load('real-auto.php');//its appear only on first window .
}, 900);
});