Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 搜索div并设置属性的jQuery代码_Javascript_Jquery_Html_Css - Fatal编程技术网

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

Javascript 搜索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

我正在编写一段代码,查找一个具有css样式的div
clear:both
,加载页面后添加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>

$(文档).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后工作。请将此添加到您的答案中。