Javascript 搜索div并设置属性的jQuery代码
我正在编写一段代码,查找一个具有css样式的divJavascript 搜索div并设置属性的jQuery代码,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在编写一段代码,查找一个具有css样式的divclear:both,加载页面后添加cssdisplay: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="tex
clear:both
,加载页面后添加cssdisplay: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>
$(文档).load(函数(){
如果($(“div”).css('clear').toLowerCase()=='both'){
$(this.css(“显示”、“无”);
});
});
检查
这是显而易见的
这是不可见的。
出于某种原因,这段代码似乎不起作用……我对jQuery有点陌生。有人能指出我的错误吗?你可以使用下面的css()
方法
$('div').css('display',function(){
if($(this.css('clear')=='both')
返回“无”
});代码>
这是显而易见的
这是不可见的。
您可以使用如下方法
$('div').css('display',function(){
if($(this.css('clear')=='both')
返回“无”
});代码>
这是显而易见的
这不可见。
用于搜索具有特定样式的所有div:
$(function() {
$(document).find('div').each(function() {
if($(this).css('clear').toLowerCase() === 'both') {
$(this).css('display', 'none');
}
})
})
在上面的代码中,.each()
将搜索文档中的所有div,然后我们检查is css属性clear
jsidle:用于搜索具有特定样式的所有div:
$(function() {
$(document).find('div').each(function() {
if($(this).css('clear').toLowerCase() === 'both') {
$(this).css('display', 'none');
}
})
})
在上面的代码中,.each()
将搜索文档中的所有div,然后我们检查is css属性clear
Jsfiddle:为所有需要的元素提供一个类不是更容易吗?然后$(“.new_class”).hide()代码>?假设我不能更改div…那么我就不能向div添加类。噢。那么阿齐姆的答案就是那个!给所有需要的元素赋予一个类不是更容易吗?然后$(“.new_class”).hide()代码>?假设我不能更改div…那么我就不能向div添加类。噢。那么阿齐姆的答案就是那个!使用$(文档).ready
而不是$(文档).load
。看到这个@I.Am.A.GuyYes,在我将load更改为ready后工作。请将此项也添加到您的答案中。使用$(文档).ready
而不是$(文档).load
。看到这个@I.Am.A.GuyYes,在我将load更改为ready后工作。请将此添加到您的答案中。