我正在尝试自定义现有函数,添加一行以检查元素是否具有类。
注意:以,
结尾的行在原始函数中,在添加hasClass
检测之前,它可以正常工作。
function SomeFunction() {
var items = $('.some-class');
items.each(function () {
var item = $(this),
children = $(this).find('.content-item'),
childrenLength = $(this).find('.content-item').length,
itemDelayCounter = 0,
if( $(this).hasClass('some-other-class') ) {
itemWidth = "100%",
} else {
itemWidth = "50%",
}
thisDraggableWrapper = item.parent(),
draggableOffset = thisDraggableWrapper.offset(),
box = {
x1: draggableOffset.left + (thisDraggableWrapper.outerWidth() - itemWidth),
y1: draggableOffset.top + (thisDraggableWrapper.outerHeight() - item.outerHeight()),
x2: draggableOffset.left,
y2: draggableOffset.top
};
item.draggable({
containment: [box.x1 + 120, box.y1, box.x2, box.y2 ],
axis: "x"
});
item.appear(function() {
$(this).css({'width': itemWidth, 'transition': childrenLength*1.3 + 's', 'pointer-events': 'none'});
children.each(function() {
itemDelayCounter+=0.6;
$(this).css({'opacity': '1', 'transition': '.5s ' + itemDelayCounter + 's', 'transform': 'translateX(-66px) translateY(0)'});
});
setTimeout(function() {
item.css({'transition': '', 'pointer-events': 'auto'});
children.each(function() {
$(this).css({'transition': ''});
});
}, 3500);
}, {accX: 0, accY: 50});
});
}
我在Firefox上遇到以下错误:
SyntaxError: missing variable name
与“ hasClass”所在的行。
在Chrome上:
Uncaught SyntaxError: Unexpected token if
在同一行。
如果我执行此处建议的操作,并将所有,
替换为;
,我还会收到一个错误消息:
SyntaxError: expected expression, got ';'
行上的itemWidth = "100%";
我有点困惑,因为确实这些行应该以{{1}}结尾,但是似乎在函数列出了在内部函数中重用的一组变量的情况下,因为我们可以注意到那些变量在这里再次使用(只是我的假设)。
或者这是写JS的“另一种”方式。我不知道!
答案 0 :(得分:2)
命令结尾应为;
,而不是,
function SomeFunction() {
var items = $('.some-class');
items.each(function () {
var item = $(this);
option1 = "some value";
option2 = "some value";
if( $(this).hasClass('some-other-class') ) {
ItemWidth = "100%";
} else {
ItemWidth = "50%";
}
代码中的其他问题是您没有声明变量。
*如果在其他地方(可能是在全球范围内)声明了这些代码,请将代码放在声明的位置。