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

css - JavaScript style property is empty - Stack Overflow

programmeradmin2浏览0评论

I need to calculate the width of an element's border. If I set it explicitly (via CSS), then I can access it in JavaScript by:

    element.style.borderWidth

However, if only specify border style property (and not 'border-width') ->

    border-style: solid

Then the borderWidth property is empty. Why? My approach to calculate width is as follows:

if(element.style.borderWidth == ''){
    borderWidth = (offsetHeight - clientHeight)/2
}

Is there any other way to calculate border width whilst only setting border-style?

I need to calculate the width of an element's border. If I set it explicitly (via CSS), then I can access it in JavaScript by:

    element.style.borderWidth

However, if only specify border style property (and not 'border-width') ->

    border-style: solid

Then the borderWidth property is empty. Why? My approach to calculate width is as follows:

if(element.style.borderWidth == ''){
    borderWidth = (offsetHeight - clientHeight)/2
}

Is there any other way to calculate border width whilst only setting border-style?

Share Improve this question edited Jun 28, 2016 at 14:08 johnkavanagh 4,6742 gold badges27 silver badges37 bronze badges asked Dec 7, 2012 at 20:58 Anton PutovAnton Putov 1,9918 gold badges34 silver badges64 bronze badges 1
  • 4 What you are looking for is the puted style of the element. – ThiefMaster Commented Dec 7, 2012 at 21:01
Add a ment  | 

1 Answer 1

Reset to default 11

You can use the window.getComputedStyle for modern browsers

window.getComputedStyle(element).borderBottomWidth;

For IE pre-9 you will have to use an alternative

element.currentStyle.borderBottomWidth 
发布评论

评论列表(0)

  1. 暂无评论