简体   繁体   English

检查页面中是否存在具有特定类的Div

[英]Check if Div with specific Class exists in the page

I'm trying to modify an existing webpage by inserting my own Javascript file in it. 我试图通过在其中插入自己的Javascript文件来修改现有网页。 I'm setting a background Image for this webpage by inserting a new Div through my Javascript and setting background of that div to image url. 我通过在我的Javascript中插入新的Div并将该div的背景设置为图片网址来为此网页设置背景图片。 For centering this image on background and to make it occupy the whole webpage, I'm using 为了使该图像以背景为中心并占据整个网页,我正在使用

'background-size': 'cover'

property. 属性。 This of course doesn't work in older IE versions. 当然,这在较旧的IE版本中不起作用。

I want to fix it, I'm thinking of zooming in the image by specific ratio calculated using page height/width and image height/width. 我要修复它,我正在考虑按使用页面高度/宽度和图像高度/宽度计算出的特定比例放大图像。

So I want to detect if browser is IE. 所以我想检测浏览器是否为IE。 I checked the webpage and it has this code that detects browser and add's a div with class IE. 我检查了网页,并使用此代码检测浏览器并添加了IE类的div。

<!--[if lte IE 5]>
  <div class="ie">
<![endif]-->
<!--[if IE 6]>
  <div class="ie ie6">
<![endif]-->
<!--[if IE 7]>
  <div class="ie ie7">
<![endif]-->

My questions is how do I detect if this page has a div with class ie. 我的问题是如何检测此页面是否具有class即div的div。 Also, I would really appreciate if someone could suggest an alternative way of setting a background image on the webpage page that occupies it fully irrespective of dimensions of image/page and image center is focused. 另外,如果有人可以建议在网页页面上设置完全占据背景页面的背景图像的另一种方法,而不管图像/页面的尺寸和图像中心是否聚焦,我将不胜感激。

You'd have to loop through all of the <div> elements on the page, until one matches the className you are looking for. 您必须遍历页面上的所有<div>元素,直到找到您要查找的className为止。 Something like: 就像是:

var div = document.getElementsByTagName('div');
for(i=0; i<div.length; i++) {
    if(div[i].className.match(/(^|\s)ie(\s|$)/)) {

        // Code to Do your IE Stuff Here

        break;
    }
}
document.addEventListener("DOMContentLoaded", function() {
  var ie = document.getElementsByClassName("ie");
  if(ie.length > 0) {
    alert("It's IE!");
  }
}

This should answer your first question, although I agree that there are better solutions for your overall goal. 这应该可以回答您的第一个问题,尽管我同意可以为您的总体目标提供更好的解决方案。

If you load the jQuery library in your page, which you can link from the Google CDN, this is a very simple matter. 如果您在页面中加载jQuery库(可以从Google CDN链接到该库),这是一件非常简单的事情。

CDN: https://developers.google.com/speed/libraries/devguide#jquery CDN: https//developers.google.com/speed/libraries/devguide#jquery

Usage: $("div.className").first() 用法:$(“ div.className”)。first()

Otherwise this will require some more effort ... the best possible way to deal with this is add an id attribute to the div element like so: 否则,这将需要更多的努力...解决此问题的最佳方法是向div元素添加一个id属性,如下所示:

<div id="myElement"></div>

And then use the document.getElementById function to retrieve your div tag. 然后使用document.getElementById函数检索div标签。

document.getElementById('myElement');

To see if it has a specific class after this addition please refer to this other post: Test if an element contains a class? 要查看添加后是否具有特定的类,请参阅另一篇文章: 测试元素是否包含类?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM