javascript - 从 jQuery 到原生 javascript
问题描述
我有这段使用 jQuery 的代码,我希望它是一个普通的 javascript。
我使用了自我可执行功能并摆脱了$
. 好吧,一旦我摆脱$
了匿名函数之前的第一个并用香草JS重写它,它就会停止工作......
$(function() {
$("#toc").append("<div id='generated-toc'></div>");
$("#generated-toc").tocify({
extendPage: true,
context: "#content",
highlightOnScroll: true,
hideEffect: "slideUp",
hashGenerator: function(text, element) {
return $(element).attr("id");
},
smoothScroll: false,
theme: "none",
selectors: $("#content").has("h1").size() > 0 ? "h1,h2,h3,h4,h5" : "h2,h3,h4,h5",
ignoreSelector: ".discrete"
});
var handleTocOnResize = function() {
if ($(document).width() < 768) {
$("#generated-toc").hide();
$(".sectlevel0").show();
$(".sectlevel1").show();
} else {
$("#generated-toc").show();
$(".sectlevel0").hide();
$(".sectlevel1").hide();
}
}
$(window).resize(handleTocOnResize);
handleTocOnResize();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
所以我这样写......但由于某种原因它不起作用......
(function() {
document.getElementById("toc").append("<div id='generated-toc'></div>");
document.getElementById("generated-toc").tocify({
extendPage: true,
context: "#content",
highlightOnScroll: true,
hideEffect: "slideUp",
hashGenerator: function(text, element) {
return element.attr("id");
},
smoothScroll: false,
theme: "none",
selectors: document.getElementById("content").has("h1").size() > 0 ? "h1,h2,h3,h4,h5" : "h2,h3,h4,h5",
ignoreSelector: ".discrete"
});
var handleTocOnResize = function() {
if (document.width() < 768) {
document.getElementsByName("generated-toc").hide();
document.getElementsByClassName("sectlevel0").show();
document.getElementsByClassName("sectlevel1").show();
} else {
document.getElementById("generated-toc").show();
document.getElementsByClassName("sectlevel0").hide();
document.getElementsByClassName("sectlevel1").hide();
}
}
window.resize(handleTocOnResize);
handleTocOnResize();
})();
解决方案
注意:我不得不降级 jQuery 以匹配 tocify
还在依赖 jQuery 的时候重写 jQuery 有什么意义?
到目前为止没有访问 HTML 的结果
- jQuery 也不起作用 - .size 在 jQuery 3.0 中已被删除。
请改用 .length 属性。翻译为
document.querySelectorAll("#content h1").length
-香草没有has
- 您的
(function() {
意思是您必须在文档之后添加 JS。而是使用window.addEventListener("load",function() {
- 附加不是香草
element.attr
不是香草。element.getAttribute("id")
要不就element.id
show
/hide
不是香草。您需要classList.toggle("hide")
或使用媒体查询或设置hidden
属性element.resize
不是香草。window.addEventListener("resize", handleTocOnResize);
是或element.onresize
- getElementsByName 在 ID 上无效,如果元素具有名称,则将返回节点列表,这不是 div 上的有效属性。
getElementsByClassName
您不能更改节点列表上的类 - 我更改为querySelector
- document.width 不是普通的。
window.addEventListener("load", function() {
document.getElementById("toc").innerHTML += "<div id='generated-toc'></div>";
const $genToc = $("#generated-toc"); // seems it MUST be a jQuery object
$genToc.tocify({
extendPage: true,
context: "#content",
highlightOnScroll: true,
hideEffect: "slideUp",
hashGenerator: function(text, element) {
return element.id;
},
smoothScroll: false,
theme: "none",
selectors: document.querySelectorAll("#content h1").length > 0 ? "h1,h2,h3,h4,h5" : "h2,h3,h4,h5",
ignoreSelector: ".discrete"
});
var handleTocOnResize = function() {
// https://gist.github.com/joshcarr/2f861bd37c3d0df40b30
const w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0],x=w.innerWidth||e.clientWidth||g.clientWidth;
const show = x < 768 // or use media queries
// $genToc[0].classList.toggle("hide", !show);
document.querySelector(".sectlevel0").classList.toggle("hide", show);
document.querySelector(".sectlevel0").classList.toggle("hide", show);
}
window.addEventListener("resize", handleTocOnResize);
handleTocOnResize();
});
.hide {
display: none
}
.tocify-header {
font-style: italic;
}
.tocify-subheader {
font-style: normal;
font-size: 90%;
}
.tocify ul {
margin: 0;
}
.tocify-focus {
color: #7a2518;
background-color: rgba(0, 0, 0, 0.1);
}
.tocify-focus > a {
color: #7a2518;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tocify/1.9.0/javascripts/jquery.tocify.min.js"></script>
<div id="content">
<h1>Toc</h1>
<p class="sectlevel0">Level 0</p>
<p class="sectlevel1">Level 1</p>
</div>
<div id="toc"></div>
jQuery测试版看看能不能让原代码工作
const handleTocOnResize = function() {
const show = $(document).width() < 768;
$("#generated-toc").toggle(!show);
$(".sectlevel0").toggle(show);
$(".sectlevel1").toggle(show);
};
$(function() {
$("#toc").append("<div id='generated-toc'></div>");
$("#generated-toc").tocify({
extendPage: true,
context: "#content",
highlightOnScroll: true,
hideEffect: "slideUp",
hashGenerator: function(text, element) {
return $(element).attr("id");
},
smoothScroll: false,
theme: "none",
selectors: $("#content h1").length > 0 ? "h1,h2,h3,h4,h5" : "h2,h3,h4,h5",
ignoreSelector: ".discrete"
});
$(window).on("resize", handleTocOnResize);
handleTocOnResize();
});
.hide {
display: none
}
.tocify-header {
font-style: italic;
}
.tocify-subheader {
font-style: normal;
font-size: 90%;
}
.tocify ul {
margin: 0;
}
.tocify-focus {
color: #7a2518;
background-color: rgba(0, 0, 0, 0.1);
}
.tocify-focus>a {
color: #7a2518;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tocify/1.9.0/javascripts/jquery.tocify.min.js"></script>
<div id="content">
<h1>Toc</h1>
<p class="sectlevel0">Level 0</p>
<p class="sectlevel1">Level 1</p>
</div>
<div id="toc"></div>
推荐阅读
- hadoop - 如何在 aws athena 中创建结构数组 - 镶木地板数据上的配置单元
- yii - 如何在yii2的下拉选择期间显示数据库列值
- c# - 玩家没有看正确的方向
- spring - 有没有办法通过检测 ACK 文件来处理文件
- amazon-web-services - Sagemaker boto3 invoke_endpoint - 我不断收到有效负载的类型错误。使用 Blazingtext 模型端点
- python - 为什么我在使用 pip 安装 python 包时没有获得最新版本的 python 包?
- ms-access - 如何在 Microsoft Access 中将多个表(作为子字段)关联到一个表(作为主字段)?
- javascript - 如何更新(添加和删除)redux 嵌套列表中的项目:Redux、React
- java - java.lang.NumberFormatException:对于输入字符串:“”用于将编辑文本中给出的字符串转换为长字符串
- bash - 使用 SSH 从私有仓库自动克隆 Git