检查页面中是否存在具有特定类别的Div [英] Check if Div with specific Class exists in the page

查看:120
本文介绍了检查页面中是否存在具有特定类别的Div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过在其中插入我自己的Javascript文件来修改现有网页。我通过插入一个新的div通过我的Javascript和设置背景的div到图像url设置此网页的背景图片。为了让这张图片在背景上居中,并使其占据整个网页,我使用了

I'm trying to modify an existing webpage by inserting my own Javascript file in it. 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. For centering this image on background and to make it occupy the whole webpage, I'm using

'background-size': 'cover'

属性。这当然不能在旧的IE版本中工作。

property. This of course doesn't work in older IE versions.

我想修复它,我想用特定比例放大图像按计算使用页面高度/宽度和图片高度/宽度。

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.

所以我想检测浏览器是否为IE。我检查了网页,它有这个代码,检测浏览器和添加的类与IE的div。

So I want to detect if browser is IE. I checked the webpage and it has this code that detects browser and add's a div with class IE.

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

我的问题是如何检测此页面是否有div类ie。此外,我真的很感激,如果有人可以建议一个替代的方式设置背景图像的网页页面占据它完全,无论图像/页面和图像中心的维度集中。

My questions is how do I detect if this page has a div with class ie. 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.

推荐答案

如果您在网页中加载jQuery库,您可以从Google CDN链接,这是一件很简单的事情。

If you load the jQuery library in your page, which you can link from the Google CDN, this is a very simple matter.

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

用法:
$(div.className)。first()

Usage: $("div.className").first()

否则,这将需要更多的努力...最好的方式来处理这是添加一个id属性到div元素,如:

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="myElement"></div>

然后使用document.getElementById函数检索您的div标签。

And then use the document.getElementById function to retrieve your div tag.

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?

这篇关于检查页面中是否存在具有特定类别的Div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆