Script JS仅在HTML页面上运行,是否有办法使其运行并包括HTML路径?

huangapple go评论77阅读模式
英文:

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">&nbsp;</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.

        &lt;link href=&quot;{% static &#39;js/animation.js&#39; %}&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;!--test active type writing animation--&gt;
&lt;div class=&quot;container-animation&quot;&gt;
&lt;p id=&quot;p_type&quot;&gt;I&#39;m &lt;span class=&quot;typed-text&quot;&gt;&lt;/span&gt;&lt;span class=&quot;cursor&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
     &lt;!-- To check , how to implement the script in js file and why it isn&#39;t working and only working--&gt;
&lt;script&gt;
const typedTextSpan = document.querySelector(&quot;.typed-text&quot;);
const cursorSpan = document.querySelector(&quot;.cursor&quot;);
const textArray = [&quot;a Data Analyst&quot;, &quot;a Developer&quot;, &quot;Henry Dumont&quot;];
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 &lt; textArray[textArrayIndex].length) {
if(!cursorSpan.classList.contains(&quot;typing&quot;)) cursorSpan.classList.add(&quot;typing&quot;);
typedTextSpan.textContent += textArray[textArrayIndex].charAt(charIndex);
charIndex++;
setTimeout(type, typingDelay);
}
else {
cursorSpan.classList.remove(&quot;typing&quot;);
setTimeout(erase, newTextDelay);
}
}
function erase() {
if (charIndex &gt; 0) {
if(!cursorSpan.classList.contains(&quot;typing&quot;)) cursorSpan.classList.add(&quot;typing&quot;);
typedTextSpan.textContent = textArray[textArrayIndex].substring(0, charIndex-1);
charIndex--;
setTimeout(erase, erasingDelay);
}
else {
cursorSpan.classList.remove(&quot;typing&quot;);
textArrayIndex++;
if(textArrayIndex&gt;=textArray.length) textArrayIndex=0;
setTimeout(type, typingDelay + 1100);
}
}
document.addEventListener(&quot;DOMContentLoaded&quot;, function() { // On DOM Load initiate the effect
if(textArray.length) setTimeout(type, newTextDelay + 250);
});
&lt;/script&gt;

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 ??

Script JS仅在HTML页面上运行,是否有办法使其运行并包括HTML路径?

I expect to call the script in the header and be working instead to be hard coded in the html page.

答案1

得分: 0

首先,您的链接指向了一个样式表

    &lt;link href=&quot;{% static &#39;js/animation.js&#39; %}&quot; rel=&quot;stylesheet&quot;/&gt;

而不是

    &lt;script src=&quot;{% static &#39;js/animation.js&#39; %}&quot; &gt;&lt;/script&gt;

但是即使假设这是一个复制错误,因为脚本引用了页面中的元素,这些元素需要在脚本运行之前加载。将脚本放在 `&lt;p&gt;` 标签下,它们将首先加载。但是如果将脚本放在头部,它将在页面的其余部分加载之前运行,这意味着您的查询选择器(例如 `document.querySelector(&quot;.typed-text&quot;)`)将找不到任何内容。

避免这种情况的方法是在头部调用脚本时使用 [defer][1],例如

    &lt;script src=&quot;{% static &#39;js/animation.js&#39; %}&quot; defer &gt;&lt;/script&gt;

这应该可以防止脚本在页面加载之前运行。

  [1]: https://www.w3schools.com/tags/att_script_defer.asp
英文:

For one thing your link is refering to a stylesheet

&lt;link href=&quot;{% static &#39;js/animation.js&#39; %}&quot; rel=&quot;stylesheet&quot;/&gt;

rather than

&lt;script src=&quot;{% static &#39;js/animation.js&#39; %}&quot; &gt;&lt;/script&gt;

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 &lt;p&gt; 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(&quot;.typed-text&quot;)) won't find anything.

The way to avoid this is to use defer when you call the script in the header, eg,

&lt;script src=&quot;{% static &#39;js/animation.js&#39; %}&quot; defer &gt;&lt;/script&gt;

This should prevent the script running until the page has loaded.

huangapple
  • 本文由 发表于 2023年1月9日 04:13:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定