javascript - 为什么 Safari 不能管理这个动态的 css Style

标签 javascript css safari

这似乎在 FF、Opera、IE (10) 甚至 chrome 中都可以正常工作,但 safari 却没有?

<html> <head>

<style> .moveable_image {position:absolute; top:50px;} </style>

<script type="text/javascript">
function move_image() { 
var image_top = 200
document.styleSheets[0].cssRules[0].style.top = image_top;
} </script>

</head> <body> 
<input type='button' onClick='move_image()' value='move image'/> 
<img class="moveable_image" src="f/4thumb.jpg">

</body> </html>

它是这样工作的:

document.styleSheets[0].cssRules[0].style.top = "200px"

但在调用“var”时不是吗?

如果我(可以)走名称路线,它可以调用相同的 var:

document.images['moveable'].style.top = image_top; 

但我需要改变整个类(class)

这是直播:http://generationsinc.co.uk/test/safari_java_cssrule_test.html

我希望这是有道理的,我根本无法找到任何内容。我什至刚刚重新安装了 Safari...

我意识到为什么我需要以这种特定方式实现这一点并不明显,但那是因为我已经从元素的核心中剔除这种烦恼并尽可能简单地呈现它。

伙计们,我有哪些选择?

编辑;在浏览器上似乎有更多的不兼容性,只有一点点旧......请参阅我的评论。

有没有我错过的更好的方法来动态更改整个类的元素?

或者仅仅是一大堆元素?

最佳答案

您可以使用 jquery 设置 css 属性 top: 200px;。看到这段代码-

<!DOCTYPE html>
<html> <head>

<style> 
.moveable_image {position:absolute; width: 100px ; top:50px;}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
function move_image() {
$('.moveable_image').css({"top":"200px"});
} </script>

</head> <body> 
<input type='button' onClick='move_image()' value='move image'/> 
<img class="moveable_image" src="Tupils.jpg">

</body> </html>

这适用于 IE、Chrome 和 Safari。

关于javascript - 为什么 Safari 不能管理这个动态的 css Style,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17299202/

相关文章:

css - 当元素到达底部边缘时设置不透明度

Safari 5.1 从内存中卸载标签

javascript - 如何在页面中添加、删除或交换 jQuery 验证规则?

javascript - 使用jquery将php数组发送到外部页面

网格中灯光序列的Javascript问题

CSS : overflow : auto will not work under FireFox 3. 6.2

javascript - 如何在 Intellij IDEA 中按位置模式自动注入(inject)语言?

javascript - 跨浏览器 Dom 就绪

html - Bootstrap 导航栏将登录按钮放在 Safari 中的第二行

jQuery 样式未在 Safari 中应用