Имеет ли значение где указывать стили?
Приветствую всех!
Подскажите какое имеет значение где указывать стили, какой приоритет? Ситуация следующая, когда стили указываю отдельно в css файле или теге <style>
<style>
#spoiler{
background: url('plus.gif');
margin:0 auto;
height:200px;
width:200px;
}
</style>
<div id="spoiler" onclick="show()"></div>
<script type="text/javascript">
function show() {
var background = document.getElementById("spoiler").style.backgroundImage;
alert (background);
}
</script>
то свойство style.backgroundImage пустое (алерт выводит пустое сообщение) А когда стиль указываю в самом теге, то все работает
<div id="spoiler" onclick="show()" style="margin:0 auto; height:200px; width:200px; background: url('plus.gif');">
<script type="text/javascript">
function show() {
var background = document.getElementById("spoiler").style.backgroundImage;
alert (background);
}
</script>
В этом алерт выводит 'plus.gif', проверял в FF, Chrome, Opera |
Если в самом теге написано, то свойство style существует, а если из css то его нет, лучше пользоваться getComputedStyle()
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
<style type="text/css">
#spoiler{background:url('1.jpg');margin:0 auto;height:200px;width:200px;}
</style>
</head>
<body>
<div id="spoiler" onclick="show(this)">Спойлер)</div>
<script type="text/javascript">
function show(self) {
var style=window.getComputedStyle?window.getComputedStyle(self,''):self.currentStyle;
alert(style.backgroundImage);
}
</script>
</body>
</html>
|
Цитата:
<button onclick="alert(this.style + ' ;' + this.style.cssText)">click</button> Your, есть кнопочка run [html run] |
Спасибо за ответы, буду разбираться.
|
bes, везде поправил.
|
| Часовой пояс GMT +3, время: 06:06. |