英文:
Script JS only working on HTML page , is there a way to make it work and include the path to HTML?
问题
我有一个问题,我有一个包含HTML 'p'标签的问题,在这里应该应用JavaScript函数。
这是一个动画打字脚本,但似乎不起作用。
只有当我将它放在HTML 'P'标签下面时,脚本才能正常工作。
<link href="{% static 'js/animation.js' %}" rel="stylesheet"/>
<!--测试活动打字动画-->
<div class="container-animation">
<p id="p_type">I'm <span class="typed-text"></span><span class="cursor"> </span></p>
</div>
<!--要检查如何在js文件中实现脚本以及为什么它不起作用,仅在位于script html标签之间工作-->
<script>
const typedTextSpan = document.querySelector(".typed-text");
const cursorSpan = document.querySelector(".cursor");
const textArray = ["a Data Analyst", "a Developer", "Henry Dumont"];
const typingDelay = 100;
const erasingDelay = 100;
const newTextDelay = 2000; // 当前文本和下一个文本之间的延迟
let textArrayIndex = 0;
let charIndex = 0;
function type() {
if (charIndex < textArray[textArrayIndex].length) {
if(!cursorSpan.classList.contains("typing")) cursorSpan.classList.add("typing");
typedTextSpan.textContent += textArray[textArrayIndex].charAt(charIndex);
charIndex++;
setTimeout(type, typingDelay);
}
else {
cursorSpan.classList.remove("typing");
setTimeout(erase, newTextDelay);
}
}
function erase() {
if (charIndex > 0) {
if(!cursorSpan.classList.contains("typing")) cursorSpan.classList.add("typing");
typedTextSpan.textContent = textArray[textArrayIndex].substring(0, charIndex-1);
charIndex--;
setTimeout(erase, erasingDelay);
}
else {
cursorSpan.classList.remove("typing");
textArrayIndex++;
if(textArrayIndex>=textArray.length) textArrayIndex=0;
setTimeout(type, typingDelay + 1100);
}
}
document.addEventListener("DOMContentLoaded", function() { // 在DOM加载时启动效果
if(textArray.length) setTimeout(type, newTextDelay + 250);
});
</script>
为什么代码如果保存在文件中并在头部脚本中调用时不起作用。它只在写在script HTML标签之间时才起作用?
英文:
I have an issue where I have a html 'p' tags where it should applies a javascript function.
It is an animated typing script , however it doesn't seems to be working.
The script only works if I put it below the html 'P' tags.
<link href="{% static 'js/animation.js' %}" rel="stylesheet"/>
<!--test active type writing animation-->
<div class="container-animation">
<p id="p_type">I'm <span class="typed-text"></span><span class="cursor">&nbsp;</span></p>
</div>
<!-- To check , how to implement the script in js file and why it isn't working and only working-->
<script>
const typedTextSpan = document.querySelector(".typed-text");
const cursorSpan = document.querySelector(".cursor");
const textArray = ["a Data Analyst", "a Developer", "Henry Dumont"];
const typingDelay = 100;
const erasingDelay = 100;
const newTextDelay = 2000; // Delay between current and next text
let textArrayIndex = 0;
let charIndex = 0;
function type() {
if (charIndex < textArray[textArrayIndex].length) {
if(!cursorSpan.classList.contains("typing")) cursorSpan.classList.add("typing");
typedTextSpan.textContent += textArray[textArrayIndex].charAt(charIndex);
charIndex++;
setTimeout(type, typingDelay);
}
else {
cursorSpan.classList.remove("typing");
setTimeout(erase, newTextDelay);
}
}
function erase() {
if (charIndex > 0) {
if(!cursorSpan.classList.contains("typing")) cursorSpan.classList.add("typing");
typedTextSpan.textContent = textArray[textArrayIndex].substring(0, charIndex-1);
charIndex--;
setTimeout(erase, erasingDelay);
}
else {
cursorSpan.classList.remove("typing");
textArrayIndex++;
if(textArrayIndex>=textArray.length) textArrayIndex=0;
setTimeout(type, typingDelay + 1100);
}
}
document.addEventListener("DOMContentLoaded", function() { // On DOM Load initiate the effect
if(textArray.length) setTimeout(type, newTextDelay + 250);
});
</script>
why the code doesn't work if it saves in a file and call in the header script. It only works when the code is written between script html tags ??
I expect to call the script in the header and be working instead to be hard coded in the html page.
答案1
得分: 0
首先,您的链接指向了一个样式表
<link href="{% static 'js/animation.js' %}" rel="stylesheet"/>
而不是
<script src="{% static 'js/animation.js' %}" ></script>
但是即使假设这是一个复制错误,因为脚本引用了页面中的元素,这些元素需要在脚本运行之前加载。将脚本放在 `<p>` 标签下,它们将首先加载。但是如果将脚本放在头部,它将在页面的其余部分加载之前运行,这意味着您的查询选择器(例如 `document.querySelector(".typed-text")`)将找不到任何内容。
避免这种情况的方法是在头部调用脚本时使用 [defer][1],例如
<script src="{% static 'js/animation.js' %}" defer ></script>
这应该可以防止脚本在页面加载之前运行。
[1]: https://www.w3schools.com/tags/att_script_defer.asp
英文:
For one thing your link is refering to a stylesheet
<link href="{% static 'js/animation.js' %}" rel="stylesheet"/>
rather than
<script src="{% static 'js/animation.js' %}" ></script>
But even assuming that is a copying error, because the script refers to elements in the page, those elements need to have been loaded before the script will work. Place the script under the <p>
tag, and they will have loaded first. But if you put the script in the header, it will run before the rest of the page has loaded, which means your queryselectors (eg, document.querySelector(".typed-text")
) won't find anything.
The way to avoid this is to use defer when you call the script in the header, eg,
<script src="{% static 'js/animation.js' %}" defer ></script>
This should prevent the script running until the page has loaded.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论