2016-08-13 95 views
2

尋找一些幫助,如果你知道如何,可能不是那麼困難。不幸的是,我對jQuery/JavaScript完全陌生,我不知道如何做到這一點,所以如果有人能指出我正確的方向,那會很棒!隱藏父類div的子類

我正在使用第三方Google地圖組件的Joomla網站上工作。酷組件,但有一個特定的標籤,我想隱藏。通常我只是簡單地用CSS隱藏這個元素,但是由於這個特殊的div沒有任何「ID」或「Class」,我想我不能直接用CSS來定位它。

我注意到我想隱藏的div的孩子,有一個獨特的類,所以我正在尋找一個解決方案,通過使用孩子的類名,我可以隱藏它的父母。

我想這樣的事情會做的伎倆:

$('.gm-style-iw').parent().hide(); 

然而,我的知識有限,我掙扎,我不知道,如果是涉及到我使用jQuery的代碼,或者是因爲我「M乾脆把它放錯了地方或別的東西,我做錯了... (總是有‘

Uncaught TypeError: Cannot read property 'parent' of null

’)

一點背景。這或多或少是代碼的特定部分的外觀。

<div style="cursor: default; position: absolute; left: 234px; top: 86px; z-index: 86;"> 
    <div class="gm-style-iw" style="top: 9px; position: absolute;"> 

我正在尋找一種方式來隱藏「DIV」上面「DIV CLASS =‘克式-IW’」

下面是谷歌地圖組件的一個活生生的例子。 http://83.84.140.23:8888/joomla/index.php/contact

基本上,我試圖隱藏「文本氣球」,指出「Gabbon」上(在底部在地圖)打開網頁...

任何人都可以點我在正確的方向關於如何隱藏這個?

非常感謝提前!

回答

1

Uncaught TypeError: Cannot read property 'parent' of null

發生這種情況是因爲您對jQuery函數使用了短$符號。

你的情況,你需要使用:

jQuery('.gm-style-iw').parent().hide(); 

欲瞭解更多詳情,請參閱noconflict

在你的情況下,該文件被加載後創建的元素,所以你需要MutationObserver API來聽爲新元素的可用性。

感謝Mutation_events你可以寫:

<script> 
    jQuery('#map').on('DOMNodeInserted', '.gm-style-iw', function(e) { 
     jQuery('.gm-style-iw').parent().hide(); 
    }) 
</script> 

此外,參照this article或其他不同的處理方法是:

<script> 
(function(win){ 
    'use strict'; 

    var listeners = [], 
      doc = win.document, 
      MutationObserver = win.MutationObserver || win.WebKitMutationObserver, 
      observer; 

    function ready(selector, fn){ 
     // Store the selector and callback to be monitored 
     listeners.push({ 
      selector: selector, 
      fn: fn 
     }); 
     if(!observer){ 
      // Watch for changes in the document 
      observer = new MutationObserver(check); 
      observer.observe(doc.documentElement, { 
       childList: true, 
       subtree: true 
      }); 
     } 
     // Check if the element is currently in the DOM 
     check(); 
    } 

    function check(){ 
     // Check the DOM for elements matching a stored selector 
     for(var i = 0, len = listeners.length, listener, elements; i < len; i++){ 
      listener = listeners[i]; 
      // Query for elements matching the specified selector 
      elements = doc.querySelectorAll(listener.selector); 
      for(var j = 0, jLen = elements.length, element; j < jLen; j++){ 
       element = elements[j]; 
       // Make sure the callback isn't invoked with the 
       // same element more than once 
       if(!element.ready){ 
        element.ready = true; 
        // Invoke the callback with the element 
        listener.fn.call(element, element); 
       } 
      } 
     } 
    } 

    // Expose `ready` 
    win.ready = ready; 

})(this); 

ready('.gm-style-iw', function(element) { 
    this.parentNode.style.display = 'none'; 
}) </script> 
+0

謝謝!有了這個,我確實可以刪除div,如果我在控制檯中輸入它。真棒.. 現在我需要弄清楚如何將這個包含在組件中,因爲我認爲它需要在元素加載到頁面後加載? 我打算玩這個現在,並會在這裏更新,如果我發現如何做到這一點.. :) – digibeta

+0

@digibeta答案更新,我希望它可以幫助你。讓我知道 – gaetanoM

+0

謝謝!它像一個魅力。 :)真的很感謝你放在一起的東西..我會做一個適當的閱讀你所鏈接的一切。真的想學習下次如何自己做... 無論如何,再次感謝一百萬!對此,我真的非常感激! – digibeta