2017-06-04 27 views
0

我想問問,我如何針對Jquery代碼中的特定部門。在下面的代碼中,我們的目標是「所有事情」,即使我們將其更改爲「div」,它也會針對文件中的所有分區。但是我的問題是,我們如何才能針對特定分區,因爲我想要以與班格 「粒子particles2」如何在jquery中定位div#specificDiv?

$(document).ready(function() { 
 

 
    $(document).on('click', function() { 
 
    $('body').jGravity({ 
 
     target: 'eveything', 
 
     ignoreClass: 'ignoreMe', 
 
     weight: 25, 
 
     depth: 5, 
 
     drag: true 
 
    }); 
 
    // Misc code for demo purposes 
 
    $(this).removeClass('#fountains'); 
 

 
    }); 
 
});
<div class="template-page"> </div> 
 
<div class="particles particles2 ignoreMe jGravity" id="fountains"></div>

+0

如果我理解你的問題,[你甚至不需要jQuery的爲( https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) – natanelg97

+0

閱讀此:https://api.jquery.com/category/selectors/ –

+0

解釋它,我使用先生Doob Gravity圖書館,我想針對具體的部門。請使用庫和目標特定div從一個div集合,並顯示一個例子,如果可能的話。 –

回答

1

使用類針對特定事業部定義爲選擇div.class,像這樣:

$(document).ready(function() { 
 

 
    $(document).on('click', function() { 
 
     $('body').jGravity({ 
 
      target: 'div.particles', 
 
      ignoreClass: 'ignoreMe', 
 
      weight: 25, 
 
      depth: 5, 
 
      drag: true 
 
     }); 
 
     // Misc code for demo purposes 
 
     $(this).removeClass('#fountains'); 
 

 
    }); 
 
});

0

這裏一個小片段,這將有助於您瞭解如何選擇工作:

$(document).on('click', '.particles', function() { 
 
    // code here that should be executed when particles div is clicked. 
 
    alert("You clicked me!"); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="particles">Click</div>

當你點擊'點擊'時,會彈出一個提示,但是如果你點擊文檔中的任何地方,什麼都不會發生。這是因爲,在JQuery中,我們提到點擊事件應該被點擊「.particles」,這是div的類。

如果你想使用ID,你需要使用'#'。例如: $("#ID_OF_THE_CONTAINER")

0

簡單的例子:

$(document).ready(function() { 
 
    $(document).on('click',".particles", function() { 
 
     /*$('body').jGravity({ 
 
      target: 'eveything', 
 
      ignoreClass: 'ignoreMe', 
 
      weight: 25, 
 
      depth: 5, 
 
      drag: true 
 
     }); 
 
     // Misc code for demo purposes 
 
     $(this).removeClass('#fountains');*/ 
 
     console.log("Test Click!"); 
 
    }); 
 
});
.particles{ 
 
    cursor: pointer; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="particles particles2 ignoreMe jGravity" id="fountains">Click me!</div>

0

如果你想針對特定類,把選擇的target:

$(document).ready(function() { 

    $(document).on('click', function() { 
     $('body').jGravity({ 
      target: '.particles.particles2', 
      ignoreClass: 'ignoreMe', 
      weight: 25, 
      depth: 5, 
      drag: true 
     }); 
     // Misc code for demo purposes 
     $(this).removeClass('#fountains'); 

    }); 
}); 
0

在您的情況:

$("div.particles.particles2") 

...將訪問您想要的特定div標籤。