簡體   English   中英

Mutation Observer未定義

[英]Mutation Observer is undefined

我正在嘗試修復並發布我的代碼。 我最初使用DOMNodeRemoved和DOMNodeInserted來關注我正在處理的頁面中的元素。 他們運作良好,但在IE中沒有運作。 所以我開始嘗試使用MutationObserver。

這是我在onPageInit上調用的代碼(回調寫入控制台但我禁用了它,因為IE不再支持控制台):

var callback = function(allmutations){
    allmutations.map( function(mr){
        var mt = 'Mutation type: ' + mr.type;  // log the type of mutation
        mt += 'Mutation target: ' + mr.target; // log the node affected.
        //console.log( mt );
    })
}
mo = new MutationObserver(callback),
options = {
    // required, and observes additions or deletion of child nodes.
    'childList': true, 
    // observes the addition or deletion of "grandchild" nodes.
    'subtree': true
}
alert('its alive');
mo.observe(document.body, options);

它在chrome中運行良好,但由於某種原因在IE中沒有變化。 我在頁面加載過程中收到一個消息框,上面寫着:

An unexpected error occurred in a script running on this page.
onPageInit(pageInit)
scriptname

JS_EXCEPTION
TypeError 'MutationObserver' is undefined

難道我做錯了什么? 附加信息:Page是一個netsuite頁面,運行jQuery 1.7.2(如果重要的話)

如果您需要在IE10 +(以及其他尚未支持MutationObserver瀏覽器)中檢測DOM插入,您可以使用基於animationstart事件的技巧來獲取CSS動畫,該動畫可以為不影響節點外觀的屬性設置動畫。

這項技術是由Daniel Buchner發現的,你可以看到它由David Walsh這篇文章中描述過

使其工作所需的代碼將是這樣的:

@keyframes animationName{ 
    from { outline: 1px solid transparent } 
    to { outline: 0px solid transparent } 
}
* { 
    animation-duration: 0.001s; 
    animation-name: animationName;
}

document.addEventListener('animationstart', insertionHandler, false);

這個技巧跨瀏覽器工作所需的設置非常復雜,包括所有前綴和事件監聽器名稱。 將為每個新節點調用處理程序,並且選擇要設置為動畫的屬性很難。

這就是為什么我將它包裝在庫中以使其易於使用: https//github.com/naugtur/insertionQuery

這是一個簡短的用法示例:

insertionQ('selector').every(function(element){
    //callback on every new element
});

該方法是在IE11中添加的,因此如果瀏覽器在兼容模式下運行IE11以外的任何其他方法,它將無法使用。

http://msdn.microsoft.com/en-us/library/ie/dn265034(v=vs.85).aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM