简体   繁体   中英

Get the width of li and add it to ul using jquery

I am trying to get the style attribute 'width' from li and adding it to ul.

My html code is

<div id="carousel_team" class="glide">
<ul class="slides" style="opacity: 1; width: 1464px;">
<li class="slide first active" data-slide="1" style="width: 244px;">1</l1>
<li class="slide" data-slide="2" style="width: 244px;">2</l1>
</ul>
</div>

And jQuery is

 var b = a.current();
 var ul = $("#carousel_team ul");
 var clicked_slide = ul.find("[data-slide='" + b + "']");
 var clicked_slide_width = ul.li:[data-slide='" + b + "']").attr("width");
 alert(clicked_slide_width);

I am getting 'Undefined'. The value of b is fine in jquery but for some reason I can't get the attribute width value.

Any help is highly appreciated. Thanks in advance.

Use this script:

_slider = $("#carousel_team ul");
_current = _slider.find('.active');
_slider.width(_slider.width() + _current.width());

This would take the width from li for the data-slide attr and append it to ul

var dataSlide = 1;
$("ul").attr("style",$("li[data-slide=" + dataSlide + "]").attr("style"));

http://jsfiddle.net/27wL3mkq/

Updated :

dataSlide = 2;
$("ul").css("width",parseInt($("li[data-slide=" + dataSlide + "]").css("width").replace("px",""))  + 
     parseInt($("ul").css("width").replace("px","")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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