gpt4 book ai didi

javascript - 搜索 div 并设置属性的 jQuery 代码

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:41 24 4
gpt4 key购买 nike

我正在编写一段代码,在页面加载后寻找具有 css 样式 clear: both 的 div,然后添加 css display: none

这是我的代码示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js" type="text/javascript"></script>
<script>
$(document).load(function () {
if( $("div").css('clear').toLowerCase() == 'both') {
$(this).css("display", "none");
});
});
</script>
<title>Check</title>
</head>
<body>
<div>This is visible</div>
<div style="text-align: center; font-size: smaller; clear: both;">This is not visible.</div>
</body>
</html>

出于某种原因,这段代码似乎无法运行...我是 jQuery 的新手。有人可以指出我的错误吗?

最佳答案

您可以使用 css() 方法,如下所示。

$('div').css('display', function () {
if ($(this).css('clear') == 'both')
return 'none'
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>This is visible</div>
<div style="text-align: center; font-size: smaller; clear: both;">This is not visible.</div>

更新:使用$(document).ready代替$(document).load

关于javascript - 搜索 div 并设置属性的 jQuery 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36680314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com