gpt4 book ai didi

Javascript 和 HTML 似乎无法正常工作

转载 作者:行者123 更新时间:2023-11-28 14:54:11 25 4
gpt4 key购买 nike

我正在尝试创建一个网页,您将鼠标悬停在其上的单词会消失,并且在您离开后再也不会出现,除了几个单词之外。我从另一个 StackOverflow 人员那里获得了大部分代码,但我在让 Javascript 与 HTML 一起实际工作时遇到了困难。

JS 中的警报只是为了检查 javascript 是否正常工作。也许我错误地将它包装在 document.ready 中?

如果我是个十足的傻瓜,我很抱歉。我不是编码员,我只是想为英语课做一个“电子文本”项目。任何帮助,将不胜感激。谢谢。这是我的代码:

HTML:

<!DOCTYPE html>
<head>
<link rel = "stylesheet" href = "NarrativeTheory.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type = "text/javascript" src="NarrativeTheory.js"></script>
<h1>
Narrative Theory Project
</h1>
</head>

<body>
hover around in the area below...
<br>
<br>

<p class = "hover">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In pharetra risus nec maximus rutrum. Vestibulum vulputate, elit ac euismod gravida, felis erat eleifend felis, vel blandit lorem ex sit amet est. Cras luctus bibendum dolor, vel consequat magna.
Morbi pellentesque turpis metus. Pellentesque sit amet erat ex. Integer et nisi nisl. Quisque ornare mollis velit, id elementum elit pharetra at. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur
cursus cursus dolor, eu laoreet mauris dapibus vitae. Nunc ac ipsum sit amet diam suscipit lobortis. Nam nec vehicula augue. Cras nec sapien vitae leo gravida vestibulum.
</p>
<p class = "hover">
Etiam viverra bibendum aliquet. Aenean erat ligula, commodo id aliquet vel, eleifend ac orci. Aliquam blandit libero feugiat augue tincidunt, id fringilla lectus aliquam. Nulla ut nisl sit amet nulla feugiat porta. Curabitur euismod, mi vitae luctus facilisis,
est risus ornare erat, sed efficitur justo lorem nec urna. Cras in fringilla dolor. Aliquam faucibus scelerisque nunc, et rutrum quam pharetra ac. Vestibulum velit enim, consequat id nisi in, laoreet feugiat turpis. Phasellus auctor pharetra ultrices.
In ut condimentum lectus. Integer at dui egestas, ultrices metus pulvinar, venenatis mi. Donec vitae mauris viverra, convallis urna sit amet, posuere sapien. Sed quis magna odio. Vivamus mauris ipsum, euismod non sagittis eu, pretium et neque. Nunc
consequat ipsum eget magna facilisis mattis. Nulla eu lorem id tortor faucibus placerat.
</p>

</body>
</html>

CSS:

* {

}

.hover {
opacity: 0.01;
}

.hovering {
opacity: 1;
}

span {
transition: opacity 0.5s;
opacity: 0;
}

p {
cursor: default;
}

Javascript:

$document.ready(function() {

alert("Help!");

var exceptions = ["lorem", "ipsum", "consectetur", "pharetra"];

$("p").each(function() { //for all paragraphs

var txt = $(this).text() //get text, split it up, add spans where necessary, put it back together
.split(" ");
.map(function(x) { return exceptions.includes(x.toLowerCase()) ? x : "<span class='hover'>" + x + "</span>"});
.join(" ");
$(this).html(txt); //set the text to our newly manipulated text

}).on("mouseover", ".hover", function() {

$(this).addClass("hovering"); //set opacity to 100%

}).on("mouseout", ".hovering", function() {

$(this).attr("class", ""); //set opacity to 0%, remove "hover" events

});
});

最佳答案

你的实际代码有几个问题:

  1. 首先它是 $(document) 而不是 $document

  2. 然后,在您的代码中,您尝试在新行中链接一些方法,例如,当您编写 .split(""); 时,只能在对象或数组上调用方法在 javascript 中,因此您需要像这样更改代码:

<小时/>
$("p").each(function() {
var txt = $(this).text()
.split(" ")
.map(function(x) {
return exceptions.includes(x.toLowerCase()) ? x : "<span class='hover'>" + x + "</span>"
}).join(" ");
$(this).html(txt);
});

注意:

  • 请注意,您不能调用 .join() 或任何其他方法(除了在 window 范围内声明的函数)在使用分号 ; 之后在上一行中,因为 ; 表示语句结束,所以在下一行中,您需要调用 object数组
  • 另请注意,您可以在浏览器控制台中检查出现的错误,以便调查您的代码并找出问题所在。

一些有用的链接:

我建议你可以阅读MDN - Introducing JavaScript objectsThe Javascript Guide to Objects, Functions, Scope, Prototypes and Closures进一步阅读有关此主题的教程。

关于Javascript 和 HTML 似乎无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43507099/

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