gpt4 book ai didi

javascript - 这段代码试图获取最接近它的元素有什么问题

转载 作者:行者123 更新时间:2023-11-28 08:40:17 24 4
gpt4 key购买 nike

此脚本(JavaScript 和 jquery)无法正常运行。

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<!DOCTYPE html>
<script>
mycars = {};

function dodat(){
var btn=document.createElement("div");
btn.style.width="25px";
btn.style.height="25px";
btn.style.backgroundColor="red";
btn.style.boxShadow="inset 0px 0px 0px 2px black";
btn.style.position="absolute";
btn.style.left="0px";
var numba = Math.round(Math.random()*10000000000);
btn.id=numba;
mycars[numba] = -100;

var move = function(){
mycars[numba] = mycars[numba]+1;
document.getElementById(numba).style.left=mycars[numba]+"px";
};

setInterval(move, 10);

document.getElementById("track").appendChild(btn);
}
</script>

<body>

<div style="background-color:#c3c3c3;width:500px;height:25px;overflow:hidden;position:relative;" id="track"></div>

<div id="shoot"></div>
</body>

<script>
setInterval("dodat();", "1000");
</script>

<script>
function dis(){
// Let's find the closest block!
var otherEls = $('div:not(#shoot):not(#track)'),
compareTop = compareEl.offset().top,
compareLeft = compareEl.offset().left,
winningScore = Infinity,
score, winner, curEl;

otherEls.each(function() {
// Calculate the score of this element
curEl = $(this);
score = Math.abs(curEl.offset().top - compareTop) + Math.abs(curEl.offset().left - compareLeft);
if (score < winningScore) {
winningScore = score;
winner = this;
}
});

alert('' + winner.id + '. Let me colour it green for you.');
}
setTimeout("dis();", "1000");
</script>

脚本的最后一部分尝试利用 jQuery 来获取最接近 #shoot 的元素,但是我一定做错了什么。非常感谢:)我想知道出了什么问题!我评估了这个函数,以防万一这是问题所在。

最佳答案

多个错误。 CompareEl 未定义,并且您的脚本在文档准备好之前运行。修复此问题后,您将无法使用全局范围。

固定代码:http://jsfiddle.net/5fJrx/

$(document).ready(function () {
mycars = {};
compareEl = $("#shoot");

function dodat() {
var btn = document.createElement("div");
btn.style.width = "25px";
btn.style.height = "25px";
btn.style.backgroundColor = "red";
btn.style.boxShadow = "inset 0px 0px 0px 2px black";
btn.style.position = "absolute";
btn.style.left = "0px";
var numba = Math.round(Math.random() * 10000000000);
btn.id = numba;
mycars[numba] = -100;

var move = function () {
mycars[numba] = mycars[numba] + 1;
document.getElementById(numba).style.left = mycars[numba] + "px";
};

setInterval(move, 10);

document.getElementById("track").appendChild(btn);
}

setInterval(dodat, 1000);

function dis() {
// Let's find the closest block!
var otherEls = $('div:not(#shoot):not(#track)'),
compareTop = compareEl.offset().top,
compareLeft = compareEl.offset().left,
winningScore = Infinity,
score, winner, curEl;

otherEls.each(function () {
// Calculate the score of this element
curEl = $(this);
score = Math.abs(curEl.offset().top - compareTop) + Math.abs(curEl.offset().left - compareLeft);
if (score < winningScore) {
winningScore = score;
winner = this;
}
});

alert('' + winner.id + '. Let me colour it green for you.');
}
setTimeout(dis, 1000);
});

关于javascript - 这段代码试图获取最接近它的元素有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20575262/

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