比如我有一个<div class="aa">阿斯顿</div>
页面载入后,jq会给他加一个z-index:20属性
但是如果我用下图的方式去操作该元素,是无效的。所以请问如何获取由jq动态生成的css值呢?
$('.aa').css('z-index')
里面取出的值是字符串。
首先你说 jquery 会给他加一个 z-index 属性,那么肯定是在 DOM Ready 事件之后加上的。
你这行代码不是在 DOM Ready 事件之后执行的,执行这行代码的时候页面还没加载完毕或者元素还没得到 z-index 属性。
加上 DOM ready 事件,并且把这个代码放在加属性的代码之后
.css("zIndex", "20")
来设置值.css("zIndex")
来取值
元素必须有 position 的样式表属性才可以设置 z-index 。另外,取值时必须在执行完你的设定代码后再执行比较逻辑。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Doooooooo……</title>
<script src="jquery.min.js"></script>
<style>
div.aa {
position: relative;
}
</style>
</head>
<body>
<div class="aa">content</div>
<script>
$(function() {
$(".aa").css("zIndex", "20");
if($(".aa").css("zIndex") < 100)
$(".aa").css({"width":"1000px", "height":"300px"});
}
</script>
</body>
</html>