2017-07-27 58 views
-10

我正在尋找一個小的JavaScript代碼,它將檢查一個div類,如果它不存在,那麼它會重定向頁面到JavaScript代碼中指定的網站。我想要一個有條件的重定向Javascript代碼

我有這個代碼,但我想要它在document.getElementById窗體,因爲我正在學習,我試着做代碼。下面的代碼是我在這個網站上找到的jQuery。

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

任何人都可以幫忙嗎?

+1

你的意思是'window.location.href ='''? – ThisGuyHasTwoThumbs

+1

你可以使用window.location.href ='somesite.com' –

+2

你得到的是沒有檢查一個類,它檢查一個id。 –

回答

2

要檢查某個元素上是否存在類,請使用classList.contains()。然後,你可以用location.assign()重定向,像這樣:

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

我正在尋找一個代碼,當指定的div類存在時不會重定向,但在不存在的情況下重定向。 –

+0

在這種情況下,您只需在條件開始處添加'!'來反轉邏輯。我編輯了你的答案 –

0

你的代碼的jQuery的一部分做的是尋找與使用ID selector的ID demodivclass元素。使用如下所示的document.getElementById()函數將實現相同的目的。

如果您想檢查是否存在具有特定類的div(根據您的問題),可以使用document.querySelector()函數代替。

要重定向頁面,請使用location.replacelocation.assign。差異在Difference 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"); 
    } 
} 
+0

我相信代碼正在檢查div類,如果存在然後重定向到另一個站點。 但是,我想對此案件。 代碼應該首先檢查div id。 然後,如果它不存在重定向應該發生。 如果divid不存在,那麼只應該發生重定向。 –

+0

@SumantSingh這是我答案中的第一個代碼塊。 – H77

0

首先要選擇你的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'; 
    } 
} 

祝你好運,不斷學習。

+0

不知道爲什麼這得到downvote而相同的答案得到upvote!也許再增加一點信息,因爲這本質上只是一個'僅限代碼'的答案。 –

+0

我可以編輯我的答案嗎? – Arrabidas92

+0

是的,你的答案底部是否沒有「編輯」按鈕? –

1

嘗試此代碼以檢查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"); 
} 

希望它能幫助!

+1

我相信OP不想使用jQuery – H77

+0

我需要相同的代碼,但在我的問題中所述的getelementbyid形式。這可能嗎? –

+1

使用此條件:if(document.getElementById('yourdivId')。className =='classname') –

-1
function check() { 
    if ($('#demodivclass').length < 1) { 
    window.location.href = 'somesite.com'; 
    } 

請嘗試下面的代碼,它與您的代碼類似。我只是更改了一些檢查語句的代碼。

+0

我不是在尋找jQuery代碼。我認爲你的答案是jquery。是否有可能使它成爲getelementbyid格式? –

+0

'if(!document.getElementById(「somediv」))'你可以在你的if語句中試試這個。它檢查somediv是否不存在,然後你可以做些什麼。 – Ilove112

0

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>

試試這個。我希望它能幫助你。