【问题标题】:I want a conditional redirection Javascript code我想要一个条件重定向 Javascript 代码
【发布时间】:2018-01-02 21:01:31
【问题描述】:

我正在寻找一个小的 javascript 代码,它将检查 div 类,如果它不存在,那么它会将页面重定向到 javascript 代码中指定的站点。

我有此代码,但我希望它采用document.getElementById 形式,因为我正在学习并尝试编写代码。下面的代码是我在这个网站上找到的 jQuery。

function check() {
  if ($('#demodivclass').length === 0) {
    redirect(somesite.com);
  }

谁能帮忙?

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

要检查元素上是否存在类,您可以使用classList.contains()。然后你可以用location.assign()重定向,像这样:

function check() {
  if (!document.getElementById('demodivclass').classList.contains('foo')) {
    window.location.assign('http://somesite.com');
  }
}

【讨论】:

  • 我正在寻找一个在指定 div 类存在时不会重定向但在不存在时重定向的代码。
  • 在这种情况下,您只需通过在条件开头添加! 来反转逻辑。我为你编辑了答案
【解决方案2】:

您的代码的 jQuery 部分正在做的是使用 ID selector 查找 ID 为 demodivclass 的元素。使用如下所示的document.getElementById() 函数将实现相同的效果。

如果您想检查是否存在具有特定类的 div(根据您的问题),可以使用 document.querySelector() 函数。

要重定向页面,请使用location.replacelocation.assignDifference between window.location.assign() and window.location.replace() 突出显示差异

如果 ID 不存在则重定向:

function check() {
  if (!document.getElementById('demodivclass')) {

    window.location.replace("somesite.com");
    // or
    window.location.assign("somesite.com");
  }
}

如果类不存在则重定向:

function check() {
  if (!document.querySelector(".demodivclass")) {

    window.location.replace("somesite.com");
    // or
    window.location.assign("somesite.com");
  }
}

【讨论】:

  • 我相信代码正在检查 div 类,如果存在则重定向到另一个站点。但是,我想与这种情况相反。代码应首先检查 div id。然后,如果它不应该发生重定向。如果没有除法,那么只应该发生重定向。
  • @SumantSingh 这就是我回答中的第一个代码块的作用。
【解决方案3】:

首先要选择您的 DOM 元素,您需要使用 getElementById 和您之前定义的 ID。然后,要检查是否有上课,您需要使用classList.contains() 方法。最后,要进行 URL 重定向,请使用 window.location.href = 'Your link'

function check() {
    if (document.getElementById('element').classList.contains('class')) {
        window.location.href = 'newPage.html';
    }
}

祝你好运,继续学习。

【讨论】:

  • 不知道为什么这得到了反对,而相同的答案得到了赞成!也许添加更多信息,否则这基本上只是一个“仅代码”的答案。
  • 是的,答案底部没有“编辑”按钮吗?
  • 哦,是的!我看到了。
  • 由于我还在学习,我可能会在理解您的代码时出错。但是,我认为这并不能满足我的需求。我需要的是一个代码,它将通过 getelementbyid 查找 div 类,然后启动条件。如果 divclass 存在,则代码不应重定向,但如果不存在,则应进行重定向。
【解决方案4】:

尝试使用此代码检查 div 是否具有特定的类名,并在条件满足后使用 JQuery 进行重定向。

JQuery

if($( "#yourdivId" ).hasClass( "classname" ))
{
  window.location.replace("https://stackoverflow.com");  
}

Javascript

if(document.getElementById('yourdivId').className == 'classname') 
{
   window.location.replace("https://stackoverflow.com");  
}

希望对你有帮助!

【讨论】:

  • 我相信OP不想使用jQuery
  • 我需要相同的代码,但采用我的问题中所述的 getelementbyid 形式。这可能吗?
  • 使用这个条件:if(document.getElementById('yourdivId').className == 'classname')
【解决方案5】:
function check() {
  if ($('#demodivclass').length < 1) {
    window.location.href = 'somesite.com';
  }

请尝试以下代码,它与您的代码相似。我只是更改了一点检查语句的代码。

【讨论】:

  • 我不是在寻找 jquery 代码。我认为你的答案是jquery。是否可以以 getelementbyid 格式制作?
  • if (!document.getElementById("somediv")) 你可以在你的 if 语句中试试这个。它检查 somediv 是否不存在然后你可以做一些事情。
【解决方案6】:

if($( "#div" ).hasClass( "divClass" )) /// check Is Exists or not
      location.assign("https://stackoverflow.com");
 else
      alert('class not exist!')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div id="div" class="divClass"></div>

试试这个。希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    相关资源
    最近更新 更多