计算div的高度

时间:2015-04-22 18:40:35

标签: javascript jquery html css

我有一个设定高度的div。

    /**
     * Serves data about current Device data to the mDeviceListView.  Manages the dynamic and
     * persistent storage of the configured Devices and constructs views of each individual
     * list item for placement in the list.
     */
    private class DeviceListAdapter extends BaseAdapter {

        private boolean mForceRedraw = false;

        /**
         * Dynamic array that keeps track of all devices currently being managed.
         * This is held in memory and is readily accessible so that system calls
         * requesting View updates can be satisfied quickly.
         */
        private List<Device> mDeviceEntries;
        private Context mContext;

        public DeviceListAdapter(Context context) {
            this.mContext = context;
            this.mDeviceEntries = new ArrayList<>();
            populateFromStorage();
        }

        /**
         * Inserts the given device into storage and notifies the mDeviceListView of a data update.
         * @param newDevice The device to add to memory.
         */
        public void put(Device newDevice) {
            Preconditions.checkNotNull(newDevice);
            boolean flagUpdatedExisting = false;
            for (Device device : mDeviceEntries) {
                if (newDevice.isVersionOf(device)) {
                    int index = mDeviceEntries.indexOf(device);
                    if(index != -1) {
                        mDeviceEntries.set(index, newDevice);
                        flagUpdatedExisting = true;
                        break;
                    } else {
                        throw new IllegalStateException();
                }
            }
            //If an existing device was not updated, then this is a new device, add it to the list
            if (!flagUpdatedExisting) {
                mDeviceEntries.add(newDevice);
            }
            TECDataAdapter.setDevices(mDeviceEntries);
            notifyDataSetChanged();
        }

        /**
         * If the given device exists in storage, delete it and remove it from the mDeviceListView.
         * @param device
         */
        public void delete(Device device) {
            Preconditions.checkNotNull(device);
            //Remove device from mDeviceEntries
            Iterator iterator = mDeviceEntries.iterator();
            while(iterator.hasNext()) {
                Device d = (Device) iterator.next();
                if(device.isVersionOf(d)) {
                    iterator.remove();
                }
            }
            TECDataAdapter.setDevices(mDeviceEntries);
            notifyDataSetChanged();
        }

        /**
         * Retrieves Device entries from persistent storage and loads them into the dynamic
         * array responsible for displaying the entries in the listView.
         */
        public void populateFromStorage() {
            List<Device> temp = Preconditions.checkNotNull(TECDataAdapter.getDevices());
            mDeviceEntries = temp;
            notifyDataSetChanged();
        }

        public int getCount() {
            if (mDeviceEntries != null) {
                return mDeviceEntries.size();
            }
            return 0;
        }

        public Object getItem(int position) {
            return mDeviceEntries.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            LinearLayout view;
            if (convertView == null || mForceRedraw) //Regenerate the view
            {

              /* Draws my views */

            } else //Reuse the view
            {
                view = (LinearLayout) convertView;
            }
            return view;
        }

        @Override
        public void notifyDataSetChanged() {
            mForceRedraw = true;
            super.notifyDataSetChanged();
            mForceRedraw = false;
        }
    }

如果我的样式属性未明确设置高度,是否可以找到div的高度?

换句话说,我想在不实际更改div的情况下找到以下div的高度。

<div class="foo" style="height:100px;">bar</div>

2 个答案:

答案 0 :(得分:4)

var clone = $('.foo').clone();
clone.css('height', 'auto');
clone.css('visibility', 'hidden');

$('body').append(clone);

console.log(clone.height());

答案 1 :(得分:1)

使用clientHeight属性获取元素的内部高度。 clientHeight等同于css height属性。请参阅下面的代码段(第一行演示了clientHeight的使用):

var height = document.getElementsByClassName('foo')[0].clientHeight;

document.getElementById('heightOfFoo').innerHTML = height;
<div class="foo">bar</div>

<div id="heightOfFoo"></div>