更改单击的元素并将其保存在cookie中

时间:2017-08-31 20:32:29

标签: javascript jquery html css cookies

$(document).ready(function(){
    $(".link").click(function(){  // when .link clicked 
        $(".elem").toggle(); // toggle .elem visibility 
    });
});
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(); 
		}
	});
});
.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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
	<title>new project</title>
	<link rel="stylesheet" type="text/css" href="style.css">
	<link rel="stylesheet" type="text/css" href="css/font-awesome.css">
</head>
<body>
<div class="wrapper">
	<a class="link" href="#">S.T.E.A.M. lab</a>
	<div class="elem">
		<p class="title">Choose youre hobby</p><a class="close">X</a>
		<div class="regions">	
			<a class="floating" href="#">Math</a>
			<a class="floating" href="#">Art</a>
			<a class="floating" href="#">Science</a>
			<a class="floating" href="#">Technology</a>
			<a class="floating" href="#">Engineering</a>
		</div>
		<p class="otherregion">Or choose another one:</p>
		<form>
			<input class="edit" type="text" name="add" placeholder="write some text bro">
		</form>
		<div class="top">bla bla bla bla bla :
		<p class="formtext">		&#8226 </p>
		<p class="formtext"> 		&#8226 .</p></div>
	</div>
</div>
</body>
</html>

大家好。这是我的简单窗口。已经3天努力改变S.T.E.A.M.实验室文本到浮动类中的其他文本,单击一个。如果可能将此结果保存在cookie中。更多详细信息更多详细信息更多详细信息(对不起这是stackoverflow,他们需要更多详细信息)

2 个答案:

答案 0 :(得分:1)

您必须向浮动元素添加单击侦听器并将文本保存到链接。为了设置cookie,我使用了hier vanilla JS,但你也可以使用这个http://en.cppreference.com/w/cpp/io/basic_istream/readsome

&#13;
&#13;
$(document).ready(function(){
    $(".link").click(function(){  // when .link clicked 
        $(".elem").toggle(); // toggle .elem visibility 
    });
    $('.floating').click(function(evt){
        evt.preventDefault();
        $('.link').text(event.target.textContent);
        // document.cookie = "cookiename=event.target.textContent";  
    });
});
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(); 
		}
	});
});
&#13;
.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;
}
&#13;
<!DOCTYPE html>
<html>
<head>
	<title>new project</title>
	<link rel="stylesheet" type="text/css" href="style.css">
	<link rel="stylesheet" type="text/css" href="css/font-awesome.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
</head>
<body>
<div class="wrapper">
	<a class="link" href="#">S.T.E.A.M. lab</a>
	<div class="elem">
		<p class="title">Choose youre hobby</p><a class="close">X</a>
		<div class="regions">	
			<a class="floating" href="#">Math</a>
			<a class="floating" href="#">Art</a>
			<a class="floating" href="#">Science</a>
			<a class="floating" href="#">Technology</a>
			<a class="floating" href="#">Engineering</a>
		</div>
		<p class="otherregion">Or choose another one:</p>
		<form>
			<input class="edit" type="text" name="add" placeholder="Начните вводить название">
		</form>
		<div class="top">bla bla bla bla bla :
		<p class="formtext">		&#8226 </p>
		<p class="formtext"> 		&#8226 .</p></div>
	</div>
</div>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

第1步:将点击事件添加到区域下的a标记。

步骤2:在此单击事件处理程序上获取作为主题的单击元素的内部文本 - 并存储在变量中以供显示并存储在cookie中。

步骤3:编写一个cookie保存函数,该函数接受一个字符串参数,并在步骤2中调用,传入主题名称以保存在cookie中。

 $("body").on("click" "div.regions a", function(e) {
     // Your code here that fetches the event.target.innertext or some such
    var txt = $(e.target).text(); // sample code not tested
    SetCookie(txt);
    SetStemLabText(txt) 
});

 SetCookie(txt) {
  $.cookie("Subject", text);
 }

 SetStemLabText(txt) {
  $('.link').text(txt)
 }