我需要计算元素边框的宽度.如果我明确地设置它(通过CSS),那么我可以通过以下方式在JavaScript中访问它:
element.style.borderWidth
Run Code Online (Sandbox Code Playgroud)
但是,如果只指定边框样式属性(而不是'border-width') - >
border-style: solid
Run Code Online (Sandbox Code Playgroud)
然后该borderWidth属性为空.为什么?我计算宽度的方法如下:
if(element.style.borderWidth == ''){
borderWidth = (offsetHeight - clientHeight)/2
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方法来计算边框宽度而只设置border-style?
我有一个javascript函数,当用户单击div以获取网页的当前背景颜色(我的笔记本电脑上为白色)时调用该函数:
<div id="div1" onclick="checkbkcolor(id, 1)">Just testing the web page bk color
</div>
Run Code Online (Sandbox Code Playgroud)
这是checkbkcolor()函数:
<head>
<script type="text/javascript">
// DOES NOTHING SO I COMMENTED IT OUT: document.body.style.backgroundColor = "red";
function checkbkcolor(id, val)
{
// THIS RETURNS NOTHING FOR THE COLOR -- the alert box
// only says "the document.body.style.backgroundColor is: "
// and nothing else
alert("the document.body.style.backgroundColor is: "
+ document.body.style.backgroundColor);
document.body.style.backgroundColor = "red";
// The alert box now shows 'red' for the color name, *AND*
// the web page's background has …Run Code Online (Sandbox Code Playgroud)