te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Uncaught TypeError: Cannot read property 'visibility' of undefined - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Cannot read property 'visibility' of undefined - Stack Overflow

programmeradmin4浏览0评论
function CheckavailOnload()
{
  var elems = document.getElementsByClassName('box-collateral box-related');
  for(var i = 0; i < elems.length; i++){
  if(elems.style.visibility == 'visible')
  {
     var av = document.getElementsByClassName('availability in-stock');
     for(var i = 0; i < elems.length; i++) {
     av[i].style.visibility = 'visible';
    }
  }
  else
  {
     var av = document.getElementsByClassName('availability in-stock');
     for(var i = 0; i < elems.length; i++) {
     av[i].style.visibility = 'hidden';
    }
  }
 }
}
CheckavailOnload();

here i am trying to check the visibility of div "box-collateral box-related" if it is visible i want to toggel the visibility of another paragraph with class name "availability in-stock"

function CheckavailOnload()
{
  var elems = document.getElementsByClassName('box-collateral box-related');
  for(var i = 0; i < elems.length; i++){
  if(elems.style.visibility == 'visible')
  {
     var av = document.getElementsByClassName('availability in-stock');
     for(var i = 0; i < elems.length; i++) {
     av[i].style.visibility = 'visible';
    }
  }
  else
  {
     var av = document.getElementsByClassName('availability in-stock');
     for(var i = 0; i < elems.length; i++) {
     av[i].style.visibility = 'hidden';
    }
  }
 }
}
CheckavailOnload();

here i am trying to check the visibility of div "box-collateral box-related" if it is visible i want to toggel the visibility of another paragraph with class name "availability in-stock"

Share Improve this question edited Dec 19, 2012 at 2:10 geeta battin asked Dec 18, 2012 at 17:45 geeta battingeeta battin 732 gold badges2 silver badges6 bronze badges 7
  • 2 Well right off the bat, syntax highlighting shows you are missing a quote... 'hidden'. Although that could just be a copy paste error... – Lix Commented Dec 18, 2012 at 17:47
  • and check the type of the nodes before assigning attributes to them. Some nodes don't have attributes at all =) – el Dude Commented Dec 18, 2012 at 17:50
  • Uncaught SyntaxError: Unexpected token ILLEGAL error is ing – geeta battin Commented Dec 18, 2012 at 21:51
  • @gee - Did you see my ment on the missing quote? – Lix Commented Dec 18, 2012 at 22:03
  • At the same time i want to move upward another div if'box-collateral box-related' visiblity is hidden – geeta battin Commented Dec 18, 2012 at 22:17
 |  Show 2 more ments

3 Answers 3

Reset to default 9

getElementsByClassName() always gives you a HTMLCollection object, even if it had only one member. HTMLCollection object doesn't have style property, hence you need to iterate elems in the first if to check the visibility, just like you've done further in your code.

If you're sure there's only one member, you can check it's visibility by using elems[0].style.visibility.

If you want to check the visibility of a particular element, you can give it an id and get that element using document.getElementById().


EDIT

Thanks for a fiddle, now we can have some results.

So, maybe you don't need that id, the actual problem occurs when trying to get style, if it's not explicitely assigned. To tackle this, you need to use getComputedStyle():

function CheckavailOnload() {
var elems = document.getElementsByClassName('box-collateral box-related');
    for (var i = 0; i < elems.length; i++) {
        if (getComputedStyle(elems[i]).visibility == 'visible' || getComputedStyle(elems[i]).display == 'block') {
            alert("hello");
            var av = document.getElementsByClassName('availability in-stock');
            for (var j = 0; j < av.length; j++) {
                av[j].style.visibility = 'visible';
            }
        }
        else if (getComputedStyle(elems[i]).visibility == 'hidden' || getComputedStyle(elems[i]).display == 'none') {
            var av = document.getElementsByClassName('availability in-stock');
            for (var k = 0; k < av.length; k++) {
                av[k].style.visibility = 'hidden';
            }
        }
    }
}
window.onload = CheckavailOnload;

This code will check all elements assigned to classes box-collateral box-related. A working demo at jsFiddle.

Notice also use of window.onload, which makes sure, that you're not trying to get elements before they are parsed. I switched elems to av in for...j- and for...k -loops, supposed to work correctly, if there were different number of elements in elems and av.

If the first found element with maintioned classes is the one to check, you can simply replace i with 0 in all elems[i] expressions.

If you want to check only a particular element, you can give it an id, and get a reference to it with getElementById(). In HTML:

<div id="checkThisOnly" class="box-collateral box-related">

And then in the script:

var elem = document.getElementById('checkThisOnly');
if (getComputedStyle(elem).visibility...) {
    ......
}

"elems" is a set of elements yet you try to look at its "style" property. You need to rearrange things so you loop through elems and then check the property on each one.

Also on your later loops through av - you are looking at elems.length for the scan. I think this is a bit confused.

So, as I mentioned in my ment, you seem to be missing a quote around the word "hidden". I'm going to assume that it's a copy paste error.


Here is a stripped down version of your code that demonstrates one way to achieve your goal -

function Checkavailability()
{
  var elems = document.getElementsByClassName('box-collateral box-related');
  for (var i=0; i<elems.length; i++) {
    if(elems[i].style.visibility == 'visible'){
      // do some stuff
    }
    else{
       // do some stuff
    }
  }
}

The main difference here is that we are itearating over all of the elements that getElementsByClassName returns. Note the plural in the function name -

get​Elements​ByClassName - Returns a set of elements which have all the given class names.

发布评论

评论列表(0)

  1. 暂无评论