简体   繁体   English

我应该如何初始化此变量以与jQuery一起使用?

[英]How should I initialize this variable for use with jQuery?

What I'd like to do is make a variable that is a specific class/id object. 我想做的是使一个变量成为特定的类/ id对象。 For example, on lines (5) and (8) where I do a literal reference of the ID, shoe-carousel, I want to have a variable that represents shoe-carousel instead. 例如,在第(5)和(8)行中,我对ID进行了字面引用,即鞋旋转木马,我想使用一个代表鞋旋转木马的变量。 The problem is that if I do something like: 问题是,如果我做类似的事情:

var fooCarousel = $('.shoe-carousel');

it'll work for line (5), not for line (8) 它适用于第(5)行,不适用于第(8)行

1. //initialize the slide in focus and what will be the center slide
2. var slideInFocus = document.createElement('img');
​3. 
4. //set the index of the immediate slide in focus when the page loads
5. slideInFocus.index = $('.shoe-carousel').slick('slickCurrentSlide');
6.     
7. //set the source of the immediate slide in focus when the page loads
8. slideInFocus.src = $('.shoe-carousel .slick-current img').prop('src');

To me, it seems like the issue with line 8 is that the single-quotes (') being pushed in from the variable definition are ruining it; 在我看来,第8行的问题似乎是从变量定义中插入的单引号(')破坏了它; so, what's the trick to give to a new JS/jQuery guy like me? 那么,给像我这样的新JS / jQuery家伙的诀窍是什么? Thanks! 谢谢!

There's no problem doing it the way you are suggesting, the issue is I think your implementation in line 8. How are you trying to call it? 按照您的建议进行操作没有问题,问题在于我认为第8行中的实现。您如何尝试调用它? Something like this would work well. 这样的事情会很好。 .find() will find the selector within the jquery object you are calling it from. .find()将在您从中调用选择器的jquery对象中找到选择器。

1. var $fooCarousel = $('.shoe-carousel');
2. var slideInFocus = document.createElement('img');
​3. 
4. //set the index of the immediate slide in focus when the page loads
5. slideInFocus.index = $fooCarousel.slick('slickCurrentSlide');
6.     
7. //set the source of the immediate slide in focus when the page loads
8. slideInFocus.src = $fooCarousel.find('.slick-current img').prop('src');

After first realizing I posted a not-appropriate answer, then reading comments under @Redmega's answer, I think that the most efficience response to the OP's issue is like this: 首先意识到我发布了一个不合适的答案,然后阅读了@Redmega的答案下的评论,我认为对OP问题最有效的回应是这样的:

var fooCarousel = $('.shoe-carousel');
1. //initialize the slide in focus and what will be the center slide
2. var slideInFocus = document.createElement('img');
​3. 
4. //set the index of the immediate slide in focus when the page loads
5. slideInFocus.index = fooCarousel.slick('slickCurrentSlide');
6.     
7. //set the source of the immediate slide in focus when the page loads
8. slideInFocus.src = $('.slick-current img', fooCarousel).prop('src');

While very close to the $fooCarousel.find('.slick-current img') solution, this way has probably the less performance impact. 虽然非常接近$fooCarousel.find('.slick-current img')解决方案,但这种方式对性能的影响可能较小。

Edit: actually it doesn't make any difference, as pointed by @Redmega. 编辑:实际上,它没有任何区别,如@Redmega所指出。

Warning : as pointed by others, this answer doesn't solve the OP issue. 警告 :正如其他人指出的那样,此答案不能解决OP问题。 I too quickly read the question, so the proposed code below would works only with fooCarousel containing the string id! 我太快读了这个问题,因此下面建议的代码仅适用于包含字符串id的fooCarousel

Yes it's what you guessed. 是的,这就是您的猜测。
And it's easy to workaround, using usual concatenation, like this: 使用常规的连接很容易解决,如下所示:

1. //initialize the slide in focus and what will be the center slide
2. var slideInFocus = document.createElement('img');
​3. 
4. //set the index of the immediate slide in focus when the page loads
5. slideInFocus.index = fooCarousel.slick('slickCurrentSlide');
6.     
7. //set the source of the immediate slide in focus when the page loads
8. slideInFocus.src = $(fooCarousel + ' .slick-current img').prop('src');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM