如何仅使用jQuery重写此javascript代码
我是jQuery的初学者,所以我有一个朋友帮我写了一些脚本,效果我遇到了麻烦。问题是,他几乎只熟悉Javascript,不知道如何将同一个脚本翻译成jQuery。有没有办法将相同的代码写入jQuery如何仅使用jQuery重写此javascript代码,javascript,jquery,Javascript,Jquery,我是jQuery的初学者,所以我有一个朋友帮我写了一些脚本,效果我遇到了麻烦。问题是,他几乎只熟悉Javascript,不知道如何将同一个脚本翻译成jQuery。有没有办法将相同的代码写入jQuery $(document).ready(function() { //milliseconds - change this value to modify the delay between //one text animation and the other var de
$(document).ready(function() {
//milliseconds - change this value to modify the delay between
//one text animation and the other
var delay = 1000;
//milliseconds - change this value to modify the single text
//animation speed
var timing = 2000;
animateText('creative_div', timing);
//setTimeout allow to call the function animateText after a
//certain delay (declared above)
setTimeout("animateText('motivated_div'," + timing + ");", delay);
setTimeout("animateText('skilled_div'," + timing + ");", delay * 2);
});
function animateText(divElement, timing) {
//$(divElement).style="visibility: visible";
document.getElementById(divElement).style.visibility = "visible";
$('#'+divElement).effect('drop',
{
direction:"up",
mode:"show",
distance:"400"
},
timing);
}
给你:
function animateText(id, t) {
$('#' + id)
.css('visibility', 'visible')
.effect('drop', {direction: 'up', mode: 'show', distance: '400'}, t);
}
$(function() {
var delay = 1000,
timing = 2000,
ids = ['creative_div', 'motivated_div', 'skilled_div'];
$.each(ids, function(i, id) {
setTimeout(function() { animateText(id, timing); }, delay * i);
});
});
顺便说一句,您可以使用常规for循环而不是$。每个:
for (var i = 0; i < ids.length; i++) {
setTimeout(function() { animateText(ids[i], timing); }, delay * i);
}
for(变量i=0;i
常规循环的执行速度稍快,但也稍差。这听起来像是家庭作业。在javascript代码中使用代码示例函数。此外,您已经在到处使用jQuery。没有javascript,您如何编写jQuery?@Jason-真的吗?将普通JavaScript转换为jQuery听起来像是家庭作业?这些天他们在CS系到底在教什么?@Iwburk-你怎么知道每个使用Stack的人都上大学了情人眼里出西施!它可以写成while循环:
vari=ids.length;虽然(我…){…}
@RobG是的,我这边的词选得不好。与jQuery的方法相比,for循环的级别更低,这被认为是一个缺点。你知道有没有一本书或一个网站供初学者“解释”语法中某些符号的含义?我对这一点是如此陌生,以至于我不知道*、t和I以及其他符号意味着什么。S@Sue*
是乘法运算符t
只是我给animateText
函数的第二个参数起的名字。我这样调用这个函数:animateText(id,计时)代码>-因此,值2000作为第二个参数传递到animateText
i
是$的第一个参数。每个
回调。阅读有关$的内容。每个功能。