gpt4 book ai didi

javascript - 脚本在 jsfiddle 中有效,但在站点中无效

转载 作者:行者123 更新时间:2023-11-30 05:46:30 25 4
gpt4 key购买 nike

我看到其他人问过这个问题,但我找不到适合我的答案。我不久前发布了一个问题,有人给我发了这个 jsFiddle:http://jsfiddle.net/r043v/LgXQX/1/

它完全满足了我的需要,网站上说它完全有效,但是当我将它粘贴到我的文档中时它不起作用。我读到它会在你 c+p 时添加额外的不可见字符,所以我使用了 jslint,它说我已经摆脱了它们,但它仍然无法正常工作。

这是我页面中的代码(我在第一次无法正常工作后从头开始,这样我可以确定我的代码中没有其他东西搞砸了它)

    <!DOCTYPE html>

<head>
<link rel="stylesheet" href="style3.css">
<script type=”text/javascript” src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

<script>
$("#issues > li").click(function () {
var $img = $("#theimg");
var $li = $(this);
var newsrc = "";
newsrc = $li.data("img");
if (newsrc === undefined) newsrc = "issue" + $li.index() + ".jpg";

var loadstep = 0;

function endstep() { // end of preload or hide
if (++loadstep === 2) // if the preload is done and hide too, change src and show picture
$img.attr("src", newsrc).animate({
opacity: 1
});
}

// preload picture
var $tmpimg = $("<img/>", {
src: newsrc
}).on("load", endstep);
// hide container picture
$img.animate({
opacity: 0
}, endstep);
});
</script>

</head>

css 文件只有 jsFiddle 中的内容,正文是从 fiddle 中的 html 部分复制和粘贴的。任何见解将不胜感激!

最佳答案

您需要在 $(document).ready() 处理程序中加载脚本。 Fiddle 为您添加它。因此它在那里工作。

$(document).ready(function () {
$("#issues > li").click(function () {
var $img = $("#theimg");
var $li = $(this);
var newsrc = "";
newsrc = $li.data("img");
if (newsrc === undefined) newsrc = "issue" + $li.index() + ".jpg";

var loadstep = 0;

function endstep() { // end of preload or hide
if (++loadstep === 2) // if the preload is done and hide too, change src and show picture
$img.attr("src", newsrc).animate({
opacity: 1
});
}

// preload picture
var $tmpimg = $("<img/>", {
src: newsrc
}).on("load", endstep);
// hide container picture
$img.animate({
opacity: 0
}, endstep);
});

});

关于javascript - 脚本在 jsfiddle 中有效,但在站点中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17690247/

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