document.getelementbyid css,使用document.getElementById()从css获取值。style.height javascript...

我试图通过

var high = document.getElementById("hintdiv").style.height;

alert(high);

摆脱一个div框高度值,如果属性包含div标签里我能得到这个值就好了,但它返回如果属性为空值在css部分中定义。

这很好,它显示100px为一个值。该值可以被访问。

.

.

var high = document.getElementById("hintdiv").style.height;

alert(high);

这不好,它显示一个空的警报屏幕。该值实际上是0。

#hintdiv

{

height:100px

display: none;

}

.

.

var high = document.getElementById("hintdiv").style.height;

alert(high);

但是我没有问题访问/改变“显示:无”属性是否是在该标签或在CSS部分。 div框通过属性定义方法(标签内或CSS部分)正确显示。

我也试图通过其他的变化来访问值,但没有运气

document.getElementById("hintdiv").style.height.value ----> undefined

document.getElementById("hintdiv").height ---->undefined

document.getElementById("hintdiv").height.value ----> error, no execution

任何解决方案?

TIA。

2010-06-16

Jamex

+4

请参阅http://stackoverflow.com/questions/1098349/reading-non-inline-css-style-info-from-javascript和http://stackoverflow.com/questions/1048336/cant-access-css- selectors-properties-from-javascript –

2010-06-16 01:51:50

+0

另请参阅:http://gist.github.com/369133和http://stackoverflow.com/questions/2531737/javascript-incapable-of-getting-elements-max-height-via -element-style-maxheight/ –

2010-06-16 02:14:42

+0

谢谢大家,这当然是意想不到的。我想我会为这个1(或2个元素)做内联样式。 –

2010-06-16 02:26:27

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码可以进行如下的优化: 1. 避免在函数内部操作 DOM,最好将其作为参数传递进来,这样可以提高代码的可重用性和可维护性。 ```javascript const print = (elId, cssUrl) => { const el = document.getElementById(elId) if (!el) { message.error("找不到打印内容,请检查elId") return } if (!baseInfo.orderCode) { message.error("打印正在准备,请5秒后再点击") return } const iframe = document.createElement("IFRAME") document.body.appendChild(iframe) const doc = iframe.contentWindow.document doc.write(`<LINK rel="stylesheet" type="text/css" href="${cssUrl}">`) doc.write(el.innerHTML) doc.close() iframe.contentWindow.focus() iframe.onload = () => { iframe.contentWindow.print() document.body.removeChild(iframe) } if (navigator.userAgent.indexOf("MSIE") > 0) { document.body.removeChild(iframe) } } ``` 2. 将打印的专有CSS样式和打印的内容分开处理,以便更好地维护和修改样式。 ```javascript const print = (elId, cssUrl, printContent) => { const el = document.getElementById(elId) if (!el) { message.error("找不到打印内容,请检查elId") return } if (!baseInfo.orderCode) { message.error("打印正在准备,请5秒后再点击") return } const iframe = document.createElement("IFRAME") document.body.appendChild(iframe) const doc = iframe.contentWindow.document const link = document.createElement("link") link.rel = "stylesheet" link.type = "text/css" link.href = cssUrl doc.head.appendChild(link) doc.body.innerHTML = printContent || el.innerHTML iframe.contentWindow.focus() iframe.onload = () => { iframe.contentWindow.print() document.body.removeChild(iframe) } if (navigator.userAgent.indexOf("MSIE") > 0) { document.body.removeChild(iframe) } } ``` 3. 将打印内容从 DOM 中分离出来,以便在不同的场合下方便地调用。 ```javascript const printContent = (elId) => { const el = document.getElementById(elId) if (!el) { message.error("找不到打印内容,请检查elId") return "" } return el.innerHTML } const print = (elId, cssUrl, printContent) => { if (!baseInfo.orderCode) { message.error("打印正在准备,请5秒后再点击") return } const iframe = document.createElement("IFRAME") document.body.appendChild(iframe) const doc = iframe.contentWindow.document const link = document.createElement("link") link.rel = "stylesheet" link.type = "text/css" link.href = cssUrl doc.head.appendChild(link) doc.body.innerHTML = printContent || printContent(elId) iframe.contentWindow.focus() iframe.onload = () => { iframe.contentWindow.print() document.body.removeChild(iframe) } if (navigator.userAgent.indexOf("MSIE") > 0) { document.body.removeChild(iframe) } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值