use*_*551 4 html javascript css jquery
我不知道为什么当我使用jQuery的.css()函数时它返回给我:
Uncaught TypeError: undefined is not a function
Run Code Online (Sandbox Code Playgroud)
我的代码是这样的:
HTML:
<div class="photo1 sliderPhoto">d</div>
<div class="photo2 sliderPhoto">d</div>
<div class="photo3 sliderPhoto">d</div>
<div class="photo4 sliderPhoto">d</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
.sliderPhoto{
position: absolute;
top: 0px;
left: 0px;
background-position: center center;
background-size: cover;
width: 100%;
height: 100%;
}
.photo1{
background-image: url('../photos/ph1.jpg');
z-index: 6;
}
.photo2{
background-image: url('../photos/ph2.jpg');
z-index: 6;
display: none;
}
.photo3{
background-image: url('../photos/ph3.jpg');
z-index: 6;
display: none;
}
.photo4{
background-image: url('../photos/ph4.jpg');
z-index: 6;
display: none;
}
Run Code Online (Sandbox Code Playgroud)
JQuery on $(document).ready:
$photos = [$(".photo1")[0], $(".photo2")[0], $(".photo3")[0], $(".photo4")[0]];
$photos[0].css("z-index");
Run Code Online (Sandbox Code Playgroud)
当我输入这个jQuery代码时,它不起作用:(但这个$ photos [0]返回我需要的确切div.
$photos[0].css("z-index");
Run Code Online (Sandbox Code Playgroud)
应该:
$($photos[0]).css("z-index");
Run Code Online (Sandbox Code Playgroud)
您必须将DOM元素转换回jQuery对象才能使用jQuery函数.
或者,使用jQuery简单地过滤元素可能更有效,因此不需要转换 -
$photos.first().css("z-index");
Run Code Online (Sandbox Code Playgroud)
有关.first()方法的更多详细信息,请参见http://api.jquery.com/first/