Use JS in Chrome to obtain the CSS width and height of tables and their elements

Source: Internet
Author: User

Although few tables are currently used, some writing operations still need to be used.

Today, when I was working on the F7Dialog2.0 project, I encountered the CSS width and height for getting table cells. I think it is very easy to get the width and height. I directly wrote the code to get it. Later, when the Code was written, I found that the chrome execution results were different and it was very depressing.

After finding the cause for half an hour, I finally found that the difference occurred when getting the cell CSS height in chrome.


Cause: different browsers have different execution results on tables.

Solution:
Later, we tested the CSS parameter acquisition in the TABLE and found that we performed border-collapse: collapse; setting on the TABLE, and set the display: block for TR and TD, at this time, the width and height of any element are obtained, and the browsers will be consistent. If this is not set, it can be said that the results obtained by the browsers vary.

My own mistake is that the display: block is not set for TD;

The specific test code is as follows:

<Style>
. Muheight {height: 100px; width: 200px; border-collapse: collapse ;}
. Muheight tr {display: block ;}
. Muheight td {display: block ;}
. MWR {height: 80px; width: 150px ;}
. D_left_up {height: 50px; width: 110px ;}
</Style>

<Table class = "muheight">
<Tr class = "mhowever">
<Td class = "D_left_up"> </td>
</Tr>
</Table>

<Script>
Alert (F $ (". muheight" ).css ("width "));
Alert (F $ (". muheight" ).css ("height "));
Alert (F $ (". mtranslated" ).css ("width "));
Alert (F $ (". mtranslated" ).css ("height "));
Alert (F $ (". D_left_up" ).css ("width "));
Alert (F $ (". D_left_up" ).css ("height "));
</Script>
Note: F $ (").css (" ") is just a small framework that I have compiled myself. It is the same as Jquery.

Other methods:
The results obtained by. scrollWidth are more different. At that time, because CSS was used to define the width and height in the cells I want to take, I did not study such methods in detail.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.