2016-03-28 13 views
7

, więc mój kod poniżej działa samodzielnie w jsfiddle. ale z jakiegoś dziwnego powodu .. ja dostać ten błąd konsekwentnie po wciśnięciu go na żywo serwera:/i nie mogę zrozumieć, dlaczego ...Uncaught TypeError: Nie powiodło się wykonanie "obserwuj" w "MutationObserver": parametr 1 nie jest typu 'Węzeł'

błąd:

mycodewitherror.js:23 Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'. 

js:


$(document).ready(function() { 
// The below collects user login name, new login date and time, and previous use URL 
var element = document.querySelector('.pet-name'); 
// create an observer instance 
var observer = new MutationObserver(function(mutations) { 
     var username = $('.pet-name').text(); 
     var referrer = document.referrer; 
     var date = new Date(); 
     var month = date.getUTCMonth() + 1; 
     var day = date.getUTCDate(); 
     var year = date.getUTCFullYear(); 
     var time = date.toLocaleTimeString(); 
     var formattedDate = month + '/' + day + '/' + year; 
    console.log("Pet Name Time"); 
     console.log(referrer); 
     console.log(petname); 
     console.log(time); 
     console.log(formattedDate); 
}); 

// configuration of the observer: 

var config = { attributes: true, childList: true, characterData: true }; 

// pass in the target node, as well as the observer options 
observer.observe(element, config); 
+0

W obu odwołaniach "element"? –

+0

Nie, tylko gdy nazwiesz 'obserwator.observe()'. Oh, czekaj, nie, całkowicie się mylę. – Pointy

+0

Cóż .... Touche ' –

Odpowiedz

4

i napotkał ten sam błąd i rozwiązany poprzez wprowadzenie sposobu .Należy() pod onLoad/gotowy ins tead definicji obserwator var plus definicja docelowego (element) i config zmienne:

$(document).ready(function() { 
 
    var target = document.getElementById("myList"); 
 
    var config = { 
 
     childList: true, 
 
     subtree: true, 
 
     attributes: true, 
 
     characterData: true 
 
    }; 
 
//note this observe method 
 
    observer.observe(target, config); 
 
    console.log("registered"); 
 
}); 
 

 
var observer = new MutationObserver(function (mutationRecords, observer) { 
 
    mutationRecords.forEach(function (mutation) { 
 
     console.log("mutation change in ", mutation.type, " name: ",mutation.target); 
 
    }); 
 
}); 
 

 
function add() { 
 
    var index = $("ul li").length; 
 
    var listItem = document.createElement("li"); 
 
    listItem.textContent = index + 1; 
 
    var target = document.getElementById("myList").appendChild(listItem, "before"); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<body > 
 
    <button onclick="add()">Add list item</button> 
 
    <hr> 
 
    <ul id="myList"> 
 
     <li><a href="#">1</a></li> 
 
     <li><a href="#">2</a></li> 
 
    </ul> 
 
    
 
</body>

uruchom fragment kodu, kliknij przycisk „Dodaj nowy element listy” i obserwować zmianę zaloguj się do konsoli.

Powiązane problemy