Jump to content
  • 0

Не получается получить свойство через DOM!


lazybaby
 Share

Question

Собственно код:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<style type="text/css">

#foo { display:none; }

</style>

</head>

<body>

<div id="foo">Some text</div>

<script type="text/javascript">

alert(document.getElementById("foo").style.display);

</script>

</body>

</html>

Ожидаю, естественно, получить "none", а в итоге - пустая строка.

При этом если задать атрибут прямо в тэге (т.е. style="display:none") получаем то, что и требуется.

Идеи?

Edited by lazybaby
Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

Может так?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
/*#foo { display:none; }
.foo { display:none; }*/
</style>
</head>
<body>
<div id="foo" class="foo">Some text</div>
<script type="text/javascript">
document.getElementById("foo").style.display="none";

//alert(document.getElementById("foo").class);


if(document.getElementById("foo").style.display=="none"){
alert("none");

}else{
alert("нет данных");

}
</script>
</body>
</html>

Link to comment
Share on other sites

  • 0

Вот хороший пример, но ИЕ не понимает его.

<style type="text/css">
.someClass {
margin-left: 15px;
}
</style>
<div onclick="alert(window.getComputedStyle(this, null).marginLeft)" class="someClass">Нажми на меня</div>

А вот для ИЕ

<style type="text/css">
.someClass {
margin-left: 15px;
}
</style>
<div onclick="alert(this.currentStyle.marginLeft)" class="someClass">Нажми на меня</div>

Link to comment
Share on other sites

  • 0

Searcher, у меня есть вопрос

Вот этот метод принимает два аргумента

alert(window.getComputedStyle(this, null)

Первый я понимаю для чего, this - ссылается на сам элемент, а зачем нужен null?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy