让我们说我需要三个阵列,福特,雪佛兰和闪避.每个数组有三个项目:
$ford['engine'] = 'something';
$ford['color'] = 'something';
$ford['price'] = 'something';
$chevy['engine'] = 'something';
$chevy['color'] = 'something';
$chevy['price'] = 'something';
$dodge['engine'] = 'something';
$dodge['color'] = 'something';
$dodge['price'] = 'something';
Run Code Online (Sandbox Code Playgroud)
我可以写出来没有问题,也不需要太长时间.但是,让我说我需要五六十个阵列,我需要制作十到二十个不同的项目.我想在每个数组的文件顶部放一个变量来表示数组名称是什么,但我不太清楚语法,我不太熟悉$$或者我将如何使用数组:
$name = 'ford';
$(I want this to be "$name")['engine'] = 'something';
$(I want this to be "$name")['color'] = 'something';
$(I want this to be "$name")['price'] = 'something';
Run Code Online (Sandbox Code Playgroud)
我也考虑过这样做:
$name = 'ford';
$view[$name.'_engine']
$view[$name.'_color']
$view[$name.'_price']
Run Code Online (Sandbox Code Playgroud)
我可以就最佳方法获得一些建议吗?
IE9在IE7模式下,我收到以下错误.使用小计数脚本:
SCRIPT1028:预期的标识符,字符串或数字
码
$.fn.countTo.defaults = {
from: 0, // the number the element should start at
to: 100, // the number the element should end at
speed: 1000, // how long it should take to count between the target numbers
refreshInterval: 100, // how often the element should be updated
decimals: 2, // the number of decimal places to show
onUpdate: null, // callback method for every time the element is updated,
onComplete: null, // callback method …Run Code Online (Sandbox Code Playgroud) 希望是一个非常简单的问题.我有一些不同的js函数编写如下:
var app={
start:function(){
//do some stuff and call calculate
}, //end start
calculate:function(){
//do some more stuff
} //end calculate
}; //end var app
var neato={
go:function(){
//do some stuff and call creation
}, //end go
creation:function(){
//do some more stuff
} //end creation
}; //end var neato
Run Code Online (Sandbox Code Playgroud)
然后我可以从以下开始:
$(document).ready(app.start);
$(document).ready(neato.go);
Run Code Online (Sandbox Code Playgroud)
有没有什么方法可以在一个文件就绪请求中组合启动两个功能?尽管尝试了几种不同的可能性,我似乎无法弄明白.
我使用以下代码向PHP脚本发出请求:
$.ajax({
method: "POST",
url: "myAPI.php",
data: {
orderById: 2,
action: 'returnStuff',
},
success: function(data){
$.each(data.data, function(key, value) {
var $targetToMove = $('.shape.'+value.attr_name);
//if element already exists on page, move it to the end of the container
if($('.'+value.xml_name).length){
$('.container').append($targetToMove);
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的返回数据的简化示例
{"data":{"0":{"id":"1","name":"This","color":"blue"},
"1":{"id":"2","name":"That","color":"red"},
"2":{"id":"3","name":"whatever","color":"blue"}}}
Run Code Online (Sandbox Code Playgroud)
如果orderById等于1,则数据从id的最低值到最高值以数字形式返回.如果它等于2,则从最高到最低以数字方式返回,如下所示:
{"data":{"0":{"id":"3","name":"whatever","color":"blue"},
"1":{"id":"2","name":"That","color":"red"},
"2":{"id":"1","name":"This","color":"blue"}}}
Run Code Online (Sandbox Code Playgroud)
我们的想法是API按照我想要的顺序返回数据,然后在ajax调用成功时,元素按返回的数据对象的顺序重新排列在页面上.
这适用于我打算使用Firefox的方式,但在Chrome中,每当我控制台成功记录数据时,顺序始终相同,尽管控制台指示我的API响应的顺序正确.
我错过了什么?我不知道这是否是一个缓存问题,或者我只是在我的javascript中忽略了什么.
我试图理解以下两组代码之间的区别.当我使用jquery插件定义时,我能够在自封闭函数之外访问插件.但是,如果我尝试简单地使用相同的自封闭功能设置某些对象,则它不起作用.
这有效:
(function($) {
$.fn.test = function(message) {
return this.each(function() {
$(this).text(message);
});
}
}(jQuery));
$(document).ready( function() {
$('p').test('This works!');
});
Run Code Online (Sandbox Code Playgroud)
不起作用:
(function($) {
var neato={
start:function(){
$('p').html('We must have an issue of scope or similar, this does not work');
}
}
}(jQuery));
$(document).ready(neato.start);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,有没有办法让我的对象在自封闭函数之外可用?