2017-08-31 57 views
0

更改點擊的元素,並將其保存在cookie中

$(document).ready(function(){ 
 
    $(".link").click(function(){ // when .link clicked 
 
     $(".elem").toggle(); // toggle .elem visibility 
 
    }); 
 
}); 
 
jQuery(function($){ 
 
\t $(document).mouseup(function (e){ 
 
\t \t var div = $(".link"); 
 
\t \t var second = $('.elem'); 
 
\t \t var close = $('.close'); 
 
\t \t if (!div.is(e.target) 
 
\t \t  && (second.has(e.target).length == 0 || close.is(e.target))) { 
 
\t \t \t second.hide(); 
 
\t \t } 
 
\t }); 
 
});
.wrapper { 
 
\t width: 1180px; 
 
\t margin-right: auto; 
 
\t margin-left: auto; 
 
} 
 

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

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

 
\t top: -10px; 
 
\t left: 33px; 
 

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

 
} 
 
a.floating { 
 
\t text-decoration: none; 
 
\t width: 24%; 
 
} 
 
a.floating:hover { 
 
\t text-decoration: underline; 
 
} 
 
.otherregion { 
 
\t margin: 0px 0px 10px 10px; 
 
\t padding-top: 15px; 
 
} 
 
.edit { 
 
\t border:1px solid #9E9E9E; 
 
    color: #000000; 
 
    padding: 3px; 
 
    font-size: 14px; 
 
    font-family: Verdana; 
 
    background: #FFF; 
 
    width: 90%; 
 
    height: 23px; 
 
} 
 
form { 
 
\t margin: 0px 0px 10px 10px; 
 
} 
 
.formtext { 
 
\t margin: 0px; 
 
\t padding-top: 2px; 
 
} 
 
.top { 
 
\t margin-left: 13px; 
 
\t margin-right: 38px; 
 
} 
 
.close { 
 
\t margin: -27px 0px 20px 444px; 
 
\t position: absolute; 
 
\t font-size: 18px; 
 
\t cursor: pointer; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>new project</title> 
 
\t <link rel="stylesheet" type="text/css" href="style.css"> 
 
\t <link rel="stylesheet" type="text/css" href="css/font-awesome.css"> 
 
</head> 
 
<body> 
 
<div class="wrapper"> 
 
\t <a class="link" href="#">S.T.E.A.M. lab</a> 
 
\t <div class="elem"> 
 
\t \t <p class="title">Choose youre hobby</p><a class="close">X</a> 
 
\t \t <div class="regions"> \t 
 
\t \t \t <a class="floating" href="#">Math</a> 
 
\t \t \t <a class="floating" href="#">Art</a> 
 
\t \t \t <a class="floating" href="#">Science</a> 
 
\t \t \t <a class="floating" href="#">Technology</a> 
 
\t \t \t <a class="floating" href="#">Engineering</a> 
 
\t \t </div> 
 
\t \t <p class="otherregion">Or choose another one:</p> 
 
\t \t <form> 
 
\t \t \t <input class="edit" type="text" name="add" placeholder="write some text bro"> 
 
\t \t </form> 
 
\t \t <div class="top">bla bla bla bla bla : 
 
\t \t <p class="formtext"> \t \t &#8226 </p> 
 
\t \t <p class="formtext"> \t \t &#8226 .</p></div> 
 
\t </div> 
 
</div> 
 
</body> 
 
</html>

嗨everyone.This是我簡單的window.Already3天IM奮力改變S.T.E.A.M.實驗室文本從浮動類的其他文本,哪一個被點擊。並且如果它可能將此結果保存在cookie中。更多詳細信息更多詳細信息更多詳細信息更多詳細信息(對不起,這是用於stackoverflow他們neeed更多詳細信息)

+0

您可以將您的代碼專注於引起問題的特定元素嗎? – WookieCoder

+0

@WookieCoder問題在哪裏?給我看看。 – DuuudeXX8

回答

1

您必須添加您的浮動元素的點擊偵聽器並保存文本以鏈接。對於設置cookie的我用票數香草JS,但你可以使用這個https://github.com/carhartl/jquery-cookie

$(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($){ 
 
\t $(document).mouseup(function (e){ 
 
\t \t var div = $(".link"); 
 
\t \t var second = $('.elem'); 
 
\t \t var close = $('.close'); 
 
\t \t if (!div.is(e.target) && (second.has(e.target).length == 0 || close.is(e.target))) { 
 
\t \t \t second.hide(); 
 
\t \t } 
 
\t }); 
 
});
.wrapper { 
 
\t width: 1180px; 
 
\t margin-right: auto; 
 
\t margin-left: auto; 
 
} 
 

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

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

 
\t top: -10px; 
 
\t left: 33px; 
 

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

 
} 
 
a.floating { 
 
\t text-decoration: none; 
 
\t width: 24%; 
 
} 
 
a.floating:hover { 
 
\t text-decoration: underline; 
 
} 
 
.otherregion { 
 
\t margin: 0px 0px 10px 10px; 
 
\t padding-top: 15px; 
 
} 
 
.edit { 
 
\t border:1px solid #9E9E9E; 
 
    color: #000000; 
 
    padding: 3px; 
 
    font-size: 14px; 
 
    font-family: Verdana; 
 
    background: #FFF; 
 
    width: 90%; 
 
    height: 23px; 
 
} 
 
form { 
 
\t margin: 0px 0px 10px 10px; 
 
} 
 
.formtext { 
 
\t margin: 0px; 
 
\t padding-top: 2px; 
 
} 
 
.top { 
 
\t margin-left: 13px; 
 
\t margin-right: 38px; 
 
} 
 
.close { 
 
\t margin: -27px 0px 20px 444px; 
 
\t position: absolute; 
 
\t font-size: 18px; 
 
\t cursor: pointer; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>new project</title> 
 
\t <link rel="stylesheet" type="text/css" href="style.css"> 
 
\t <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"> 
 
\t <a class="link" href="#">S.T.E.A.M. lab</a> 
 
\t <div class="elem"> 
 
\t \t <p class="title">Choose youre hobby</p><a class="close">X</a> 
 
\t \t <div class="regions"> \t 
 
\t \t \t <a class="floating" href="#">Math</a> 
 
\t \t \t <a class="floating" href="#">Art</a> 
 
\t \t \t <a class="floating" href="#">Science</a> 
 
\t \t \t <a class="floating" href="#">Technology</a> 
 
\t \t \t <a class="floating" href="#">Engineering</a> 
 
\t \t </div> 
 
\t \t <p class="otherregion">Or choose another one:</p> 
 
\t \t <form> 
 
\t \t \t <input class="edit" type="text" name="add" placeholder="Начните вводить название"> 
 
\t \t </form> 
 
\t \t <div class="top">bla bla bla bla bla : 
 
\t \t <p class="formtext"> \t \t &#8226 </p> 
 
\t \t <p class="formtext"> \t \t &#8226 .</p></div> 
 
\t </div> 
 
</div> 
 
</body> 
 
</html>

+0

非常感謝。 – DuuudeXX8

+0

沒有prbm!請注意,由於交叉來源問題,設置cookie層不起作用(我在代碼中評論了該行)。但是這對您的應用程序應該沒有問題。 –

+0

@Marouhen Mhiri你能解釋一下它是如何工作的。請問如何將結果保存到cookie中。我從github倉庫下載cookie.js,但我不知道如何使用。 – DuuudeXX8

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) 
} 
相關問題