如果存在cookie,请删除第一个jquery元素
我试图在x秒后显示另一张桌子,到目前为止我做得很好。 我只需要放置一次“x秒后等待”,所以我需要创建一个cookie,并检查cookie是否存在,只允许直接div 解释: -First time user=您可以在x秒后检查内容 -重复用户=直接转到内容 需要:能够创建cookie,检查用户是否重复=中断第一个div并直接转到第二个div。 这就是我得到的:如果存在cookie,请删除第一个jquery元素,jquery,html,Jquery,Html,我试图在x秒后显示另一张桌子,到目前为止我做得很好。 我只需要放置一次“x秒后等待”,所以我需要创建一个cookie,并检查cookie是否存在,只允许直接div 解释: -First time user=您可以在x秒后检查内容 -重复用户=直接转到内容 需要:能够创建cookie,检查用户是否重复=中断第一个div并直接转到第二个div。 这就是我得到的: <style> #picOne, #picTwo { position:absolute; display: none; }
<style>
#picOne, #picTwo {
position:absolute;
display: none;
}
#pics {
width:100px;
height:100px;
}
</style>
<script type = "text/javascript">
/*author Philip M. 2010*/
var timeInSecs;
var ticker;
function startTimer(secs){
timeInSecs = parseInt(secs)-1;
ticker = setInterval("tick()",1000); // every second
}
function tick() {
var secs = timeInSecs;
if (secs>0) {
timeInSecs--;
}
else {
clearInterval(ticker); // stop counting at zero
// startTimer(15); // remove forward slashes in front of startTimer to repeat if required
}
document.getElementById("countdown").innerHTML = secs;
}
startTimer(15); // 15 seconds
</script>
<script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#picOne').fadeIn(0500).delay(15000).fadeOut(000);
$('#picTwo').delay(15000).fadeIn(1500);
});
</script>
</head>
<body>
<div id="pics">
<div id="picOne"> Your video will begin in
<span id="countdown" style="font-weight: bold;">15</span> </div>
<div id="picTwo">//something</object>
</div>
</div>
#皮克内#皮克托{
位置:绝对位置;
显示:无;
}
#照片{
宽度:100px;
高度:100px;
}
/*作者Philip M.2010*/
时间昆虫;
var-ticker;
功能启动计时器(秒){
timeInSecs=parseInt(secs)-1;
ticker=setInterval(“tick()”,1000);//每秒
}
函数tick(){
var secs=时间单位;
如果(秒>0){
时光虫;
}
否则{
clearInterval(滴答声);//在零处停止计数
//startTimer(15);//如果需要,删除startTimer前面的前斜杠以重复
}
document.getElementById(“倒计时”).innerHTML=secs;
}
startTimer(15);//15秒
$(文档).ready(函数(){
$(#picOne').fadeIn(0500)、delay(15000)、fadeOut(000);
美元("picTwo").延迟(15000美元).法代因(1500美元);;
});
您的视频将从中开始
15
//某物
我编写了一个简单的CookieManager对象来演示如何实现这一点。在(MDN document.cookie docs)中有一个更健壮的版本来描述这种类型的东西[https://developer.mozilla.org/en-US/docs/Web/API/document.cookie]但这应该有助于你获得一个基本的理解。您可以/应该扩展它以允许域、路径、过期等
当您第一次访问该页面时,您将看到倒计时消息。当倒计时完成时,jquery用于替换#content
div的内容(第48-51行)。刷新页面时,如果存在已访问的
cookie,将立即显示内容
<!DOCTYPE html>
<html>
<head>
<title>document.cookie Example</title>
</head>
<body>
<div id="content"><p>Your content will be shown in <span id="countdown">15</span> seconds</p></div>
<a href="#" class="delete">Remove Cookie</a>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var main = new Main();
main.init();
});
var Main = function() {
var _this = this,
cookieMngr = new CookieManager(),
countInterval = false,
count = 15;
_this.init = function() {
if(cookieMngr.getCookie("visited")) {
_this.addContent();
} else {
countInterval = setInterval(_this.updateCountdown, 1000);
}
$(".delete").on("click", function(evt) {
evt.preventDefault();
cookieMngr.deleteCookie("visited");
document.location = document.location;
});
};
_this.updateCountdown = function() {
if(count > 0) {
count--;
$("#countdown").text(count);
} else {
clearInterval(countInterval);
countInterval = false;
cookieMngr.setCookie("visited", "yes");
_this.addContent();
}
};
_this.addContent = function() {
var newContent = $(document.createElement("div")).html("<p>This is the content you've been waiting for.</p>");
$("#content").empty().append(newContent);
};
return _this;
};
var CookieManager = function() {
_this = this;
_this.setCookie = function(cookieKey, cookieValue) {
var cookieStr = cookieKey+"="+cookieValue;
document.cookie = cookieStr;
};
_this.getCookie = function(cookieKey) {
var regex = new RegExp(cookieKey+"\=([^;]+)");
var cookie = document.cookie.match(regex);
if(cookie !== null) {
return cookie[1];
} else {
return false;
}
};
_this.deleteCookie = function(cookieKey) {
document.cookie = cookieKey+"=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
};
return _this;
};
</script>
</body>
</html>
document.cookie示例
您的内容将在15秒内显示
$(文档).ready(函数(){
var main=新的main();
main.init();
});
var Main=函数(){
var_this=这个,
cookieMngr=新建CookieManager(),
countInterval=false,
计数=15;
_this.init=函数(){
if(cookieMngr.getCookie(“已访问”)){
_这是addContent();
}否则{
countInterval=setInterval(\u this.updateCountdown,1000);
}
$(“.delete”)。在(“单击”,函数(evt){
evt.preventDefault();
cookieMngr.deleteCookie(“访问”);
document.location=document.location;
});
};
_this.updateCountdown=函数(){
如果(计数>0){
计数--;
$(“#倒计时”)。文本(计数);
}否则{
清除间隔(计数间隔);
countInterval=false;
cookieMngr.setCookie(“已访问”、“是”);
_这是addContent();
}
};
_this.addContent=函数(){
var newContent=$(document.createElement(“div”)).html(“这是您一直在等待的内容。”;
$(“#content”).empty().append(newContent);
};
把这个还给你;
};
var CookieManager=函数(){
_这个=这个;
_this.setCookie=函数(cookieKey、cookieValue){
var cookieStr=cookieKey+“=”+cookieValue;
document.cookie=cookieStr;
};
_this.getCookie=函数(cookieKey){
var regex=newregexp(cookieKey+“\=([^;]+)”);
var cookie=document.cookie.match(regex);
如果(cookie!==null){
返回cookie[1];
}否则{
返回false;
}
};
_this.deleteCookie=函数(cookieKey){
document.cookie=cookieKey+“=;expires=Thu,1970年1月1日00:00:00 GMT”;
};
把这个还给你;
};
(document.cookie)[您可能想签出@Jbird,如果(document.cookie.replace(/(?:(?:^^.*.\s*)someCookieName\s*=\s*([^;]*).$)^.*.$/,“$1”)!==“true”){警报(“测试”);如何使计时只显示一次?rgds document.cookie=“someCookieName=true;expires=Fri,1999年12月31日23:59:59 GMT;path==”}@Joel很抱歉,花了这么长时间才回复您;大学足球周末。我在下面的答案中添加了一些示例代码,这应该会有所帮助。@Jbird完全理解,我回答了答案。这就是我要找的,但我不能,因为我不知道如何优化我需要的东西。在第一个欢迎,第一个计时器中,我需要添加co解压和一个图像广告,在使用jquery显示内容后,在再次刷新或访问后,它应该直接指向正确的欢迎回来!您以前来过这里,我尝试使用javascript的包含选项,然后添加所需的全部内容,但失败了。只有警报和文本有效。有什么办法吗?谢谢again@Joel我更新了des这正是我想要的,我正在用wordpress尝试,但倒计时不起作用,我正在粘贴帖子部分,所以你认为我需要在单个帖子中添加吗?如果你对wordpress有经验。@Joel,这似乎是另一个问题。如果这回答了你的第一个问题uestion,你能把它标记为答案吗?是的,很抱歉。这正是我所看到的内容的副本。:)谢谢你抽出时间