gpt4 book ai didi

Javascript 获取 div/插入 Div

转载 作者:行者123 更新时间:2023-12-01 03:15:13 26 4
gpt4 key购买 nike

我正在尝试获取 ID 为removeNav 的div 并将其再次粘贴回来。但我的 div 未定义。

function init() {
var insertMenu = $("#removeNav").html();
console.log(insertMenu);

window.addEventListener('scroll', function (e) {
var distanceY = window.pageYOffset || document.documentElement.scrollTop,
shrinkOn = 300,
header = document.querySelector(".navbar-collapse"),
removeNav = document.getElementById('removeNav'),
insertNav = document.getElementById('insertNav'),
body = document.querySelector("body");

//var insertMenu = '<div id="removeNav" class="navbar-collapse collapse header"><img class="navbar-left" src="img/MRS_Logo_WEB_Interlaced.png" width="120" height="96"> <ul class="nav navbar-nav navbar-right"> <li class="divider-vertical"></li> <li><a data-toggle="modal" data-target="#myModal" href="#about">Login</a></li> <li class="divider-vertical"></li> <li><a href="#contact">Download</a></li> <li class="divider-vertical"></li> <li class="divider-vertical"></li> <li><a href="#about">Karriere</a></li> <li><a href="#contact">Sitemap</a></li> <li class="divider-vertical"></li> <li><a href="#contact">Impressum</a></li> <li class="divider-vertical"></li> <form class="navbar-form navbar-right"> <div class="form-group"> <input class="form-control" placeholder="Suchen" type="text"> </div> <button type="submit" class="btn btn-default glyphicon glyphicon-search"></button> </form> </ul></div>';

if(distanceY > shrinkOn){
classie.add(header, "smaller");
removeNav.parentNode.removeChild(removeNav);
} else {
if (classie.has(header, "smaller")) {
classie.remove(header, "smaller");
insertNav.innerHTML = insertMenu;
}
}
});
}
window.onload = init();

我的代码: https://jsfiddle.net/t23nn1pp/

最佳答案

jsFiddle:https://jsfiddle.net/t23nn1pp/6/

我知道我的修复与您的原始代码有些不同,但是您是否尝试过隐藏和显示导航栏?

jQuery

function init() {
var $insertMenu = $("#removeNav");

window.addEventListener('scroll', function (e) {
var distanceY = window.pageYOffset;
var shrinkOn = 100;

if(distanceY > shrinkOn){
$insertMenu.hide();
} else {
$insertMenu.show();
}
});
}
window.onload = init();

例如CSS

body{
min-height: 2000px;
}

#insertNav{
width: 100%;
max-height: 200px;
background-color: #F0F;
}

Html 与您提供的相同。

关于Javascript 获取 div/插入 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45578829/

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