Uncaught TypeError: $(...).size is not a function

Uncaught TypeError: $(...).size is not a function

我正在使用插件从文件管理器中插入图像,但是出现错误:

imagebrowser.php?func=addImage&w=160&h=120:77 Uncaught TypeError: $(...).size is not a function
  at addImage (/site/dashboard/load.php?id=i18n_gallery&create:564)
  at submitLink (imagebrowser.php?func=addImage&w=160&h=120:77)
  at HTMLAnchorElement.onclick (imagebrowser.php?func=addImage&w=160&h=120:53)
  addImage @ /site/dashboard/load.php?id=i18n_gallery&create:564
  submitLink @ imagebrowser.php?func=addImage&w=160&h=120:77
  onclick @ imagebrowser.php?func=addImage&w=160&h=120:53

我正在使用的 JavaScript 代码:

    <p><em><b><?php echo count($filesSorted); ?></b> <?php i18n('TOTAL_FILES'); ?> (<?php echo fSize($totalsize); ?>)</em></p>
  <p><a href="javascript:void(0)" onclick="submitAllLinks()"><?php i18n('i18n_gallery/ADD_ALL_IMAGES'); ?></a></p>
  <?php // foreach ($metadata as &$m) if (!@$m['title']) $m['title'] = basename($m['url']); ?>
  <script type='text/javascript'>
    // <![CDATA[
    var metadata = <?php echo json_encode($metadata); ?>;
    function submitLink(i) {
      var item = metadata[i];
      if(window.opener){
        window.opener.<?php echo $func; ?>(item['url'], item['size'], item['width'], item['height'], item['title'], item['tags'], item['description']);
        <?php if ($autoclose) { ?>window.close();<?php } ?>
      }
    }
    function submitAllLinks() {
      for (var i=0; i < metadata.length; i++) {
        submitLink(i);
      }
    }
    // ]]>
  </script>

EditExtra.php

<?php
  global $id, $url, $parent;
?>
<script type="text/javascript">
  // <![CDATA[
<?php
  $pages = return_i18n_pages();
  # tags
  $tags = array();
  foreach ($pages as $page) {
    foreach (preg_split('/\s*,\s*/', trim(@$page['tags'])) as $t) $tags[$t] = true;
  }
  $tags = array_keys($tags);
  sort($tags);
  # structure
  $structure = return_i18n_page_structure(null,false,$url);
  $siblings = @$pages[''.$parent]['children'];
  if ($siblings) $myindex = array_search($url, $siblings); else $myindex = false;
  $after = $myindex !== false && $myindex > 0 ? $siblings[$myindex-1] : '';
?>
  var after = <?php echo json_encode($after); ?>;
  function changeParent() {
    $parent = $('#post-parent').val();
<?php
  $firstoption = '<option value="0">-- '.i18n_r('i18n_navigation/TOP').' --</option>';
  $page = $pages[null];
  if (isset($page['children']) && count($page['children']) > 0) {
    $children = $page['children'];
    $options = '';
    for ($i=0; $i<count($children); $i++) {
      if ($children[$i] != $url) $options .= '<option value="'.$children[$i].'">'.$pages[$children[$i]]['title'].'</option>';
    }
?>
    if ($parent == null || $parent == '') {
       $('#post-menu-order').empty().html(<?php echo json_encode($firstoption.$options); ?>);
<?php
  }
  foreach ($structure as $page) {
    $page = $pages[$page['url']];
    if (isset($page['children']) && count($page['children']) > 0) {
      $children = $page['children'];
      $options = '';
      for ($i=0; $i<count($children); $i++) {
        if ($children[$i] != $url) $options .= '<option value="'.$children[$i].'">'.$pages[$children[$i]]['title'].'</option>';
      }
?>
    } else if ($parent == <?php echo json_encode($page['url']); ?>) {
       $('#post-menu-order').empty().html(<?php echo json_encode($firstoption.$options); ?>);
<?php
    }
  }
?>
    } else {
       $('#post-menu-order').empty().html(<?php echo json_encode($firstoption); ?>);
    } 
    $('#post-menu-order').val(after);     
  }
  function changeUrl() {
    var url = $('#post-id').val();
    if ($('div.leftopt').length > 0) { // GetSimple 3.0+
      var hide = url.indexOf('_') >= 0;
      if (hide) {
        $('#post-parent').closest('p').hide();
        $('#post-private').closest('p').hide();
        $('#post-template').closest('p').hide();
        $('#post-menu-order').hide();
        $('#post-menu-order').prev().hide();
      }
    } else {
      if (url.indexOf('_') >= 0) {
        $('#post-parent').closest('tr').attr('style','display:none');
        $('#post-private').closest('td').children().attr('style','display:none');
        //$('#post-menu-enable').attr('style','display:none');
        $('#post-menu').nextAll().attr('style','display:none');
      } else {
        $('#post-parent').closest('tr').attr('style',null);
        $('#post-private').closest('td').children().attr('style',null);
        //$('#post-menu-enable').attr('style',null);
        $('#post-menu').nextAll().attr('style',null);
      }
    }
  }
  var tags = <?php echo json_encode($tags); ?>;
  $(function() {
    $fParent = $('#post-parent');
    $fParent.empty();
    $fParent.append(<?php echo json_encode('<option value="" '.($parent == null ? 'selected' : '').'>-- '.i18n_r('NONE').' --</option>'); ?>);
<?php
  foreach ($structure as $page) {
?>
    $fParent.append(<?php echo json_encode('<option value="'.$page['url'].'" '.($parent == $page['url'] ? 'selected' : '').'>'.($page['level'] > 0 ? str_repeat("&nbsp;",5*$page['level']-2).'&lfloor;&nbsp;' : '').cl($page['title']).'</option>'); ?>);
<?php
  }
?>
    if ($('#tick').length > 0) { // GetSimple 3.1+
      $('a.viewlink').hide();
      $('#menu-items').css('height','auto').css('padding-bottom','10px');
      $('#post-menu').attr('style','').addClass('short');
      $('#post-menu').prev().remove();
      $('#post-menu').prev().remove();
      $('#post-menu').prev().remove();
      $('#post-menu').before('<span style="text-transform:none"><label for="post-menu">' + <?php echo json_encode(i18n_r('MENU_TEXT')); ?> + '</label></span>');
      $('#post-menu-order').attr('style','clear:both;').addClass('short');
      $('#post-menu-order').before('<span style="text-transform:none;float:left;"><label for="post-menu">' + <?php echo json_encode(i18n_r('i18n_navigation/INSERT_AFTER')); ?> + '</label></span>');
    } else if ($('div.leftopt').length > 0) { // GetSimple 3.0
      $('label[for=post-menu-enable]').nextUntil('input').remove();
      $('#menu-items').css('height','auto');
      $('#post-menu').attr('style','').addClass('short');
      $('#post-menu').prev().remove();
      $('#post-menu').prev().remove();
      $('#post-menu').prev().remove();
      $('#post-menu').before('<label for="post-menu">' + <?php echo json_encode(i18n_r('MENU_TEXT')); ?> + '</label>');
      $('#post-menu-order').attr('style','').addClass('short');
      $('#post-menu-order').before('<label for="post-menu">' + <?php echo json_encode(i18n_r('i18n_navigation/INSERT_AFTER')); ?> + '</label>');
    } else {
      $a = $('#post-menu-enable').closest('td').find('a');
      $a.after($a.html());
      $a.remove();
      $('#post-menu').closest('div').find('span').empty().html(<?php echo json_encode(i18n_r('MENU_TEXT')); ?>);
      $('#post-menu').attr('style','').after(<?php echo json_encode('<br /><span>'.i18n_r('i18n_navigation/INSERT_AFTER').'</span>'); ?>);
      $('#post-menu-order').attr('style','').before('<br />');
    }  
    $('#post-parent').change(changeParent);
    $('#post-id').change(changeUrl);
    changeParent();
    changeUrl();
    $('#post-metak').autocomplete(tags, {
      minChars: 0,
      max: 50,
      scroll: true,
      multiple: true,
      multipleSeparator: ', '
    });
  });
  // ]]>
</script>

这是我调查后怀疑的主要错误原因:

if ($('#tick').size() > 0) {

但是用 length 替换它并没有解决问题。

jQuery版本:jquery-3.2.1.min.js?v=3.2.1

不要使用 $(...).size()it is deprecated as of jQuery 1.8。 请改用 .length 属性。

有关详细信息,请参阅 .size() | jQuery API Documentation


size()替换为length后,发现实际问题是引用了不存在的元素。这意味着 undefined 确实不是函数。

size() 函数已被弃用。 添加以下代码:

$(function() {
    $.fn.size = function() {
        return this.length;
    }
});

您不需要更改代码。