1) 使用 jQuery cookie 库以方便使用。https://github.com/carhartl/jquery-cookie
2) 检查cookie是否存在并进行相应处理
if ($j.cookie("NameOfYourCookie") == null) {
//Do stuff if cookie doesn't exist like set a cookie with a value of 1
$j.cookie("NameOfYourCookie", 1, {expires: 10, path:'/'});
}
else {
//They have a cookie so do something
alert ('You have a cookie with name NameOfYourCookie');
}
* 编辑 *
以为我可能会编辑我的答案以更适合您的问题。
<button class="yes">YES</button>
<button class="no">No</button>
var $j = jQuery.noConflict();
$j(document).ready(function(){
//Show modal on page load
$j("#myModal").show();
$j('.yes').click(function(){
$j("#myModal").hide();
$j.cookie("YesButtonCookie", 1, {expires: 10, path:'/'});
alert ('You have clicked yes, a cookie has been dropped');
});
if ($j.cookie("YesButtonCookie") == 1){
// Don't show the div
$j("#myModal").hide();
alert ('Cookie found, you cant see myModal');
}
if ($j.cookie("YesButtonCookie") == null){
// Cookie Not Found
alert ('Cookie Not found, you can see myModal');
$j("#myModal").show();
}
$j('.clearcookie').click(function(){
$j.cookie("YesButtonCookie", null);
alert('Cookie Cleared');
});
});
JSFIDDLE 测试