document.getElementById(...).style.display为空白 [英] document.getElementById(...).style.display is blank

查看:143
本文介绍了document.getElementById(...).style.display为空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function test( id )
{
    alert( document.getElementById( id ).style.display );
}

getElementById.style.display到底返回什么?是对象还是值?警报什么都没有显示.我没有使用纯数字作为ID,它是唯一的.

What exactly does getElementById.style.display return? Is it an object, or a value? The alert shows nothing at all. I'm not using pure numbers for the id and it is unique.

推荐答案

DOM方法(例如 document.getElementById())创建的对象指向-并包含有关特定HTMLElement或一组HTML的某些详细信息元素.

DOM methods like document.getElementById() create objects which point to- and contains certain details about- a specified HTMLElement or set of elements.

不幸的是, .style 属性仅知道使用同一功能设置的样式属性.在下面的示例中,单击什么颜色?将不起作用,直到您单击更改颜色.

Unfortunately the .style property only knows about the style properties set using that same feature. In the example below, clicking what color? will not work until you have clicked change color.

<html>
<head>
<script>
function whatColor() {
    var d = document.getElementById( 'ddd' );
    alert( d.style.backgroundColor );
}
function changeColor() {
    var d = document.getElementById( 'ddd' );
    d.style.backgroundColor='orange';
}
</script>
<style>
#ddd {
    background-color:gray;
}
</style>
</head>
<body>
<input type="button" onclick="whatColor();" value="what color?" />
<input type="button" onclick="changeColor();" value="change color" />
<div id="ddd">&nbsp;</div>
</body>
</html>

我建议阅读PKK关于getComputedStyle和currentStyle的精彩页面(IE,当然是不同的) http://www.quirksmode.org/dom/getstyles.html

I recommend reading PKK's great page on getComputedStyle and currentStyle (IE, of course is different) http://www.quirksmode.org/dom/getstyles.html

在本教程的最后,有一个非常不错的功能可以满足您的需求,尽管jQuery之类的框架确实提供了非常方便的&用于设置页面元素样式的强大功能: http://api.jquery.com/css/

At the end of the tutorial, there is a very decent function for your purposes, although frameworks such as jQuery do provide very convenient & powerful functions for styling page elements: http://api.jquery.com/css/

这篇关于document.getElementById(...).style.display为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆