gpt4 book ai didi

javascript - 在包装 div 之后插入 div

转载 作者:行者123 更新时间:2023-11-28 07:01:27 24 4
gpt4 key购买 nike

正如标题所示,我正在尝试在包装器 div 之后插入“cookie”警告。我也尝试过使用append,但它在执行其他所有操作后添加代码,因此div id包装大量代码,然后在最后cookiebar div

var cname = "consentCookie";
var cvalue = "on";
var exdays = "20";
var cookiehtml = '\
<div id="cookiebar">\
We use cookies to track usage and preferences. \
<a href="#" id="enablecookies" onclick="setCookie();">I Understand.</a>\
</div>';
function setCookie() {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
$('#cookiebar').hide();
}
function getCookie() {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}

$(document).ready(function(){
if(getCookie() == ''){
$('#wrapper:first-child').after(cookiehtml);
}
else if(getCookie() == 'on'){
$('#cookiebar').hide();
}

});

和 HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="icon" type="image/ico" href="assets/logo/logo.ico">

<link rel="stylesheet" style="text/css" href="../cookiebar/cookiebar.css">


<link rel="stylesheet" style="text/css" href="css/styles.css">
<link rel="stylesheet" style="text/css" href="css/fonts.css">
<link rel="stylesheet" style="text/css" href="css/navigation_bar.css">
<link rel="stylesheet" type="text/css" href="css/lato_font.css">

<script src="js/jquery.min.js"></script>
<script src="../cookiebar/cookiebar.js"></script>
<script src="js/jquery.color-RGBa-patch.js"></script>
<script src="js/navigation_bar.js"></script>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="League of Legends patch notes in a minimalistic and modern way.">
<meta name="keywords" content="league, of, league notes,legends, patch, notes, leaguenotes,Patch '.$Patch_No.' ">
<meta name="author" content="Kacper Turon">
<title>LeagueNotes - '.$Patch_No.'</title>
</head>
<body>
<div id="wrapper"></div>

我只留下了最重要的代码,因为它有很多

更新:我添加了用于创建 cookie 和隐藏栏的功能,到目前为止我已经尝试了所有建议,并且 cookiebar div 作为 sibling 位于包装器旁边

<div id="wrapper">
</div>
<div id="cookiebar">
</div>

我希望它成为包装器中的第一个子元素

最佳答案

首先,我不确定您是否按预期使用 :first-child 。您可能应该简单地使用 :first:eq(0)

但即便如此,您可能仍想使用 insertAfter() 而不是 after()

$(document).ready(function(){
if(getCookie() == ''){
$('#wrapper:first-child').after(cookiehtml);
}
...

因此应该是:

$(document).ready(function(){
if(getCookie() == ''){
$(cookiehtml).insertAfter( '#wrapper');
}
...

http://jsfiddle.net/grh1w80q/1/

或者,如果您希望 cookiehtml 作为父#wrapper 的第一个节点,请使用 prependTo():

$(document).ready(function(){
if(getCookie() == ''){
$(cookiehtml).prependTo( '#wrapper');
}
...

http://jsfiddle.net/grh1w80q/2/

关于javascript - 在包装 div 之后插入 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32103665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com