如何在cookie或localstorage中保存点击的结果

时间:2017-09-10 10:10:45

标签: javascript jquery html cookies local-storage

private void createPopupDialog() {
    dialogBuilder = new AlertDialog.Builder(this, R.style.MyDialogTheme);
    View view = getLayoutInflater().inflate(R.layout.dialog_download, null);
    streamButton = (Button) view.findViewById(R.id.streamButton);
    downloadButton = (Button)view.findViewById(R.id.downloadButton);
    cancelButton = (Button) view.findViewById(R.id.cancelButton);
    dialogBuilder.setView(view);
    dialog = dialogBuilder.create();
    adNativeView = (NativeExpressAdView)view.findViewById(R.id.nativeAD);
    AdRequest request = new AdRequest.Builder()
            .addTestDevice(DEVICE_ID)
            .build();
    adNativeView.loadAd(request);
    dialog.show();

}
$(document).ready(function(){
    $(".link").click(function(){
        $(".elem").toggle(); 
    });
    $('.floating').click(function(evt){
        evt.preventDefault();
        $('.link').text(event.target.textContent);
        // document.cookie = "cookiename=event.target.textContent"; this doesnt working
    });
});
jQuery(function($){
	$(document).mouseup(function (e){ 
		var div = $(".link"); 
		var second = $('.elem');
		var close = $('.close');
		if (!div.is(e.target) 
		    && (second.has(e.target).length == 0 || close.is(e.target))) { 
			second.hide(); 
		}
	});
});
// Cookies.set('cookiename','0blue'); also this one
.wrapper {
	width: 1180px;
	margin-right: auto;
	margin-left: auto;
}

.elem {
	display:none;
	margin-top: 14px;
	width: 480px;
	height: 310px;
	background-color: grey;
	border-radius: 5px;
	-moz-border-radius: 5px;
	-webkit-border-radius: 5px;
	position: relative;
	box-shadow:0 2px 3px rgba(0,0,0,0.5);
	transition: 0.4s;
}
.elem:after {
	content: '';
	position: absolute;

	width: 0;
	height: 0;
	border-bottom: 10px solid grey;
	border-left: 10px solid transparent;
	border-right: 10px solid transparent;

	top: -10px;
	left: 33px;

}
.title {
	margin: 0px 0px 10px 10px;
	padding-top: 15px;
	position: relative;
}
.link {
	margin-left: 13px;
}
.regions {
	height: 50px;
	display: inline-block;
}
.floating {
	display: inline-block;
	margin: 10px;
	line-height: 0.4;
	width: 20px;

}
a.floating {
	text-decoration: none;
	width: 24%;
}
a.floating:hover {
	text-decoration: underline;
}
.otherregion {
	margin: 0px 0px 10px 10px;
	padding-top: 15px;
}
.edit {
	border:1px solid #9E9E9E;
    color: #000000;
    padding: 3px;
    font-size: 14px;
    font-family: Verdana;
    background: #FFF;
    width: 90%;
    height: 23px;
}
form {
	margin: 0px 0px 10px 10px;
}
.formtext {
	margin: 0px;
	padding-top: 2px;
}
.top {
	margin-left: 13px;
	margin-right: 38px;
}
.close {
	margin: -27px 0px 20px 444px;
	position: absolute;
	font-size: 18px;
	cursor: pointer;
}

首先,我需要了解为什么cookie不在我的浏览器中设置。我拿了另外一个脚本并从我的openserver运行它,一切都很好,但如果我写这样的代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
	<title>Any project</title>
	<meta http-equiv="Content-Type" content="text/html"; charset="utf-8" />
</head>
<body>
<div class="wrapper">
	<a class="link" href="#">Countries</a>
	<div class="elem">
		<p class="title">Choose youre locality </p><i class="close" >X</i>
		<div class="regions">	
			<a class="floating" href="#">Kiev</a>
			<a class="floating" href="#">Baku</a>
			<a class="floating" href="#">Paris</a>
			<a class="floating" href="#">London</a>
			<a class="floating" href="#">Moscow</a>
			<a class="floating" href="#">Amsterdam</a>
			<a class="floating" href="#">Rome</a>
			<a class="floating" href="#">Dubai</a>
			<a class="floating" href="#">Berlin</a>
		</div>
		<p class="otherregion">Or choose another one:</p>
		<form>
			<input class="edit" type="text" name="add" placeholder="Начните вводить название">
		</form>


</div>
<script type="text/javascript" src="jquery/jquery-3.2.1.min.js"></script><!-- This is my cookie  -->
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="cookie.js"></script>
</body>
</html>

警告显示空字符串。在开发者工具中,Chrome显示空行。接下来我需要点击并选择(点击)它必须保存在cookie或localstorage中的国家/地区列表中的任何国家/地区。如果有可能请告诉我两个版本,如果你刷新浏览器或关闭浏览器它必须在那里。而不是国家必须是你的选择。请任何人帮助已经周无法解决这个问题。

2 个答案:

答案 0 :(得分:0)

使用localStorage。

$('.floating').click(function(e){
   e.preventDefault();
   $('.link').text($(this).html());
   localStorage.setItem('cookiename',$(this).html());
});

然后检索它

 var value = localStorage.getItem('cookiename');

答案 1 :(得分:0)

语法不正确。 document.cookie = "cookiename=" + event.target.textContent;

理想情况下,您也希望设置过期日期,否则在浏览器关闭后Cookie将不存在。

var dt = new Date();
dt.setTime(dt.getTime() + (<duration in ms>);
document.cookie = "cookiename=" + event.target.textContent + ";expires=" + dt.toUTCString();