gpt4 book ai didi

jquery - 如何通过CSS属性jQuery获取img

转载 作者:行者123 更新时间:2023-11-30 23:59:25 24 4
gpt4 key购买 nike

我想通过 jQuery 的 css 属性获取一个 img。该属性是 margin-left:-1920px。

我怎样才能做到这一点?

我尝试了$('img').css('margin-left', '-1920px');
console.log('img');
但当然它改变了属性(property),这是我不想要的。我想先获取元素,然后更改属性。如何获取元素?

我的 jQuery 代码:

$(document).ready(function() {
var sliderUL = $('.slider').children('ul'),
imgs = sliderUL.find('img'),
imgWidth = imgs[0].width, // 960
imgsLen = imgs.length, // 4
current = 1,
totalImgsWidth = imgsLen * imgWidth;


$('#slider-nav')
.show().find('button').on('click', function() {
direction = $(this).data('dir');

var loc = imgWidth;
(direction === 'next') ? ++current : --current;

if (current === 0) {
current = imgsLen;
direction = 'next';
loc = totalImgsWidth - imgWidth;


} else if ( current - 1 === imgsLen ) {
current = 1;
loc = 0;
}

transition(sliderUL, loc, direction);


});
function transition( container, loc, direction ) {
var unit;

if ( direction && loc !== 0 ) {
unit = ( direction === 'next' ) ? '-=' : '+=';
}

container.animate({
'margin-left': unit ? (unit+loc) : loc
})

}

});

我的 HTML:

<body>
<div id="slider-nav">
<button data-dir="prev"><</button>
<button data-dir="next">></button>
</div>
<div class="slider">
<ul>
<li><img src="img1.jpg" alt="image"></li>
<li><img src="img2.jpg" alt="image"></li>
<li><img src="img3.jpg" alt="image"></li>
<li><img src="img4.jpg" alt="image"></li>
</ul>
</div>

最佳答案

您可以使用 filter() 过滤具有该样式的图像。 :

var img = $('img').filter(function() {
return $(this).css('margin-left') == '-1920px';
});

关于jquery - 如何通过CSS属性jQuery获取img,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21874709/

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