最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery .hide() does not work with my array - Stack Overflow

programmeradmin1浏览0评论

Hello and thanks for your help.

I have a php script on my website that returns my last few tweets in form of an unordered list with the posts each being a list item.

<div id="latesttweet">
  <ul>
    <li>
      This is a tweet<br />
      <span>3 hours ago.</span>
    </li>
    <li>
      This is another tweet<br />
      <span>4 hours ago.</span>
    </li>
  </ul>
</div>

I tried to hide some of them dynamically using JavaScript, but for some reason once I collect all the list items in an Array, I can't address them properly anymore. When I alert the length of the Array however, I get the right number. jQuery seems to be working alright in general.

var activeTweet = 0;
var tweet_ul = document.getElementById('latesttweet');
var tweetArray = tweet_ul.getElementsByTagName('li');

var tweetCount = tweetArray.length;
alert(tweetCount); //returns the right result

tweetArray[1].hide();

This way, one should expect that the second list item would have display set to none, but still both items remain visible.

What am I doing wrong?

Hello and thanks for your help.

I have a php script on my website that returns my last few tweets in form of an unordered list with the posts each being a list item.

<div id="latesttweet">
  <ul>
    <li>
      This is a tweet<br />
      <span>3 hours ago.</span>
    </li>
    <li>
      This is another tweet<br />
      <span>4 hours ago.</span>
    </li>
  </ul>
</div>

I tried to hide some of them dynamically using JavaScript, but for some reason once I collect all the list items in an Array, I can't address them properly anymore. When I alert the length of the Array however, I get the right number. jQuery seems to be working alright in general.

var activeTweet = 0;
var tweet_ul = document.getElementById('latesttweet');
var tweetArray = tweet_ul.getElementsByTagName('li');

var tweetCount = tweetArray.length;
alert(tweetCount); //returns the right result

tweetArray[1].hide();

This way, one should expect that the second list item would have display set to none, but still both items remain visible.

What am I doing wrong?

Share Improve this question asked Aug 22, 2012 at 11:43 maxmax 1151 silver badge9 bronze badges 4
  • $(tweetArray[1]).hide(); – Mient-jan Stelling Commented Aug 22, 2012 at 11:45
  • 1 remember array starts from 0 so try $(tweetArray[0]).hide(); – Peter Commented Aug 22, 2012 at 11:46
  • Why do you use jquery for hiding an element and normal javascript for access to the element? – Pigueiras Commented Aug 22, 2012 at 11:48
  • @PeterSzymkowski - Note that the question specified hiding the second list item. – Scott Sauyet Commented Aug 22, 2012 at 11:57
Add a ment  | 

8 Answers 8

Reset to default 2

Basically you are not using jQuery

$(tweetArray[1]).hide()

Try this:

$(tweetArray[1]).hide();

Your array are DOM objects, not jQuery objects. Try the following:

var activeTweet = 0;
var tweet_ul = $('#latesttweet');
var tweetArray = $('li');

var tweetCount = tweetArray.length;
alert(tweetCount); //returns the right result

tweetArray[1].hide();
tweetArray[1].style.display="none";
$(tweetArray[1]).hide();

This should do the job. tweetArray[1] is just an html element and it does not have hide() method defined for it.

hide() is part of jQuery. You have to use the $() method: $(tweetArray[1]).hide().

You will have to wrap tweetArray[1] into a jquery object

Try

$(tweetArray[1]).hide();

var tweets = $( '#latesttweet ul' ).children( 'li' ); 
$( tweets[1] ).hide();

http://jsbin./usiyex/2/

发布评论

评论列表(0)

  1. 暂无评论