开发者

how to read hidden div using jquery?

开发者 https://www.devze.com 2023-01-18 05:42 出处:网络
$(\'.slider\').each(function(){ if($(\'li:last\',this).width()+$(\'li:last\',this).offset().left-$(\'li:first\',this).offset().left < $(\'div\',this).width()){
$('.slider').each(function(){
if($('li:last',this).width()+$('li:last',this).offset().left-$('li:first',this).offset().left < $('div',this).width()){
//doing something

}
});

I have multiple slides and only one is visible. here I'm trying to do some thi开发者_运维问答ng after reading each slides. Issue is that I'm not able to read slides if its hidden. is there any way I can read hidden div using jquery.


JQuery can read hidden elements as it will be hidden from the user but exist in the HTML, but if you means that you set it as visible=false from the server side that means that i will not render in the HTML and JQuery will not be able to find it for sure.


I take it you are trying to read dimensional properties of a hidden div and its child objects?

Since the div is hidden, dimensional properties make little sense. Try unhiding the div, and adding to its css: position: absolute; left: -5000px; before checking its dimensions and those of its children. Then revert it back to hidden and to the position and left settings it had before.


Assuming you aren't loading a new page, or removing the nodes each time you change slides, the div should be available to you in the DOM. If you know the id of the div you can get it using:

 var divContents = $('#slide2').html()

or the like. Keep in mind that this will give you the innerHTML, not text (unless there's only text in the innerHTML).


If Gus is right you need to:

  • Determine if the element is hidden
  • If the element is hidden set visiblity to hidden and display to block (or inline-block)
  • Read dimension values
  • Set style back to original

Something like:

$('.slider').each(function(){
  var lastLi = $('li:last',this);
  var firstLi = $('li:first',this);
  var divJQObject = $('div', this);
  var originalStyle = {
    'display': divJQObject.css('display'),
    'visibility': divJQObject.css('visibility')
  };
  if(!divJQObject.is(":visible")) {
    var hiddenButReadableStyle = {
      'visiblity': 'hidden',
      'display': 'block'
    }
    divJQObject.css(hiddenButReadableStyle);      
  }
  if(lastLi.width()+$lastLi.offset().left-firstLi.offset().left < divJQObject.width()){
  // Do Something
  }
  divJQObject.css(originalStyle);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消