检查div是否存在,如果不存在则重定向 [英] Check whether a div exists and redirect if not

查看:108
本文介绍了检查div是否存在,如果不存在则重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查我的网页上是否存在某个 div ,如果没有,将访问者重定向到另一个页面?

解决方案

您需要使用JavaScript才能检查元素是否存在并执行重定向。

假设这个div有一个id(例如div id =elementId),你可以简单地这样做:

  if(!document.getElementById elementId)){
window.location.href =redirectpage.html;

$ / code>

如果您使用的是jQuery,以下是解决方案:

  if($(#elementId)。length === 0){
window.location.href =redirectpage html的;
}

增加:

如果您需要检查特定单词的div内容(因为我认为这就是您现在要求的),您可以这样做(jQuery):

  $(div)。each(function(){
if($(this).text()。indexOf(copyright)> ; = 0)){
window.location.href =redirectpage.html;
}
});


How do I check whether a certain div exist on my page and if not, redirect the visitor to another page?

解决方案

You will need to use JavaScript to be able to check if the element exists and do the redirect.

Assuming the div has an id (e.g. div id="elementId") you can simply do:

if (!document.getElementById("elementId")) {
    window.location.href = "redirectpage.html";
}

If you are using jQuery, the following would be the solution:

if ($("#elementId").length === 0){
    window.location.href = "redirectpage.html";
}

Addition:

If you need to check the content of divs for a specific word (as I think that is what you are now asking) you can do this (jQuery):

$("div").each(function() {
    if ($(this).text().indexOf("copyright") >= 0)) {
        window.location.href = "redirectpage.html";
    }
});​

这篇关于检查div是否存在,如果不存在则重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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