|
|
|
| В свойствах объекта, при обращении из javascript везде undefined, хотя эти же свойства описаны в css... Не могу понять в чем ошибка.... Оъясните, пожалуйста, что не так
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
<title>Untitled Document</title>
<style type="text/css">
#banner_x {
position:absolute;
visibility:hidden;
background-color:#000000;;
z-index:0;
left:100px;
top:100px;
height:100px;
width:100px;
border: 1px solid #e4e4e4;
}
</style>
<script type="text/javascript">
function showLayer(layer) {
document.getElementById(layer).style.visibility = 'visible';
}
function hideLayer(layer) {
document.getElementById(layer).style.visibility = 'hidden';
}
function setLeft(layer) {
top_y = ((screen.width - document.getElementById(layer).style.width)/2);
document.getElementById(layer).style.left = top_y+'px';
}
function setTop(layer) {
top_x = (screen.height - document.getElementById(layer).style.height)/2;
document.getElementById(layer).style.top = top_x+'px';
}
</script>
</head>
<body id="fd">
<div id="banner_x">
<p>dffd</p>
</div>
<script>alert(document.getElementById("banner_x").style.width);</script>
<a href="#" onclick="showLayer('banner_x')">показать</a>
</body>
</html>
|
вообще все свойства недоступны в объекте, надо как то инициализировать их? | |
|
|
|
|
|
|
|
для: mistix
(18.09.2007 в 23:49)
| | А если прописываю стиль в теге див то все ок...!
С чем это может быть связано? | |
|
|
|
|
|
|
|
для: mistix
(19.09.2007 в 00:00)
| | Да, нужно инициализировать.
Изначально объект style не содержит значения
которые вы прописываете в css. | |
|
|
|
|
|
|
|
для: RMW
(19.09.2007 в 01:08)
| | а как инициализировать? | |
|
|
|
|
|
|
|
для: mistix
(19.09.2007 в 01:14)
| | Прописать. | |
|
|
|
|
|
|
|
для: mistix
(19.09.2007 в 01:14)
| | Мы можем получить значение только встроенных стилей
которые устанавливаются через атрибут style html-тегов,
либо которые установлены через одноимённое свойство js объекта.
Для того, чтобы получить свойства из таблицы стилей
можно использовать getComputedStyle() для не IE
и currentStyle для IE. | |
|
|
|
|
|
|
|
для: RMW
(19.09.2007 в 12:36)
| | А так:
document.styleSheets[0].rules[0].style
обойти в цикле отискать селектор, пройтись по директиве, взять по одной "строке" из директивы, это вроде тип DOMString и присвоить его нужному элементу страницы....
Как вариант что-то вроде этого для ИЕ:
document.getElementById('ty').style.cssText =
document.styleSheets[0].rules[0].style.cssText;
|
| |
|
|
|
|
|
|
|
для: mistix
(19.09.2007 в 23:11)
| | Разобрался, вот функция для инициализации:
function init(id) {
for (var i = 0; i <= (document.styleSheets.length - 1); i++) {
for (var j = 0;j <= (document.styleSheets[i].rules.length - 1); j++) {
selector = document.styleSheets[i].rules[j].selectorText;
if (selector == '#'+id)
{
document.getElementById(id).style.cssText =
document.styleSheets[i].rules[j].style.cssText;
break;
}
}
}
}
init('banner');
|
Подразумевается, что селектор идентификатора таблицы стилей совпадает с идентиф. элемента страницы | |
|
|
|
|
|
|
|
для: mistix
(20.09.2007 в 00:10)
| |
function init(id) {
if (navigator.userAgent.indexOf("MSIE") != -1){
cssRules = 'rules';
}
if (navigator.userAgent.indexOf("Firefox") != -1){
cssRules = 'cssRules';
}
for (var i = 0; i <= (document.styleSheets.length - 1); i++) {
total_length = eval('document.styleSheets[i].'+cssRules+'.length');
for (var j = 0;j <= total_length - 1; j++) {
selector = eval('document.styleSheets[i].'+cssRules+'[j].selectorText');
if (selector == '#'+id)
{
if (document.getElementById(id)) {
document.getElementById(id).style.cssText =
eval('document.styleSheets[i].'+cssRules+'[j].style.cssText');
break;
}
}
}
}
}
|
Этот код работает на IE и в Gecko... Как заставить его работать в Опере? | |
|
|
|
|
|
|
|
для: mistix
(20.09.2007 в 00:25)
| | Вот окончательный вариант функции инициализации может кому пригодится:
function init(id) {
if (navigator.userAgent.indexOf("MSIE") != -1){
cssRules = 'rules';
}
if ((navigator.userAgent.indexOf("Firefox") != -1) ||
(navigator.userAgent.indexOf("Opera") != -1)) {
cssRules = 'cssRules';
}
for (var i = 0; i <= (document.styleSheets.length - 1); i++) {
total_length = eval('document.styleSheets[i].'+cssRules+'.length');
for (var j = 0;j <= total_length - 1; j++) {
selector = eval('document.styleSheets[i].'+cssRules+'[j].selectorText');
if (selector == '#'+id)
{
if (document.getElementById(id)) {
document.getElementById(id).style.cssText =
eval('document.styleSheets[i].'+cssRules+'[j].style.cssText');
break;
}
}
}
}
}
|
Работает в IE7, Opera9, Firefox2, Netscape9 | |
|
|
|