3

我有一個問題不能自行解決。我正在調試這幾天,但沒有找到成功,發現問題在哪裏。jQuery選擇:未捕獲NotFoundError:無法執行'節點'上的'appendChild':

我有一個chosen.js + jscrollpane.js。當一個選擇被打開時,搜索輸入字段必須被集中以寫入一些搜索字符串。我第一次打開所選內容會拋出一個錯誤,我無法在其中搜索任何內容。但如果我關閉選擇並重新打開它,一切正常。

我做一個搗鼓你: http://jsfiddle.net/y2h3ohr3/2/

所以錯誤,當我打開第一次選擇,我可以在Chrome中看到的控制檯這個錯誤(在Firefox中,存在錯誤,但控制檯不拋出錯誤):

Uncaught NotFoundError: Failed to execute 'appendChild' on 'Node': The node to be removed is no longer a child of this node. Perhaps it was moved in a 'blur' event handler?

跟蹤該控制檯拋出:

jQuery.extend.buildFragment @ jquery-1.9.1.js:6541

jQuery.fn.extend.domManip @ jquery-1.9.1.js:6129

jQuery.fn.extend.append @ jquery-1.9.1.js:5949

initialise @ jquery.jscrollpane.min.js:115

JScrollPane @ jquery.jscrollpane.min.js:1388

(anonymous function) @ jquery.jscrollpane.min.js:1407

jQuery.extend.each @ jquery-1.9.1.js:648

jQuery.fn.jQuery.each @ jquery-1.9.1.js:270

$.fn.jScrollPane @ jquery.jscrollpane.min.js:1399

(anonymous function) @ chosen.jquery.js:774

jQuery.event.dispatch @ jquery-1.9.1.js:3074

elemData.handle @ jquery-1.9.1.js:2750

jQuery.event.special.focus.trigger @ jquery-1.9.1.js:3256

jQuery.event.trigger @ jquery-1.9.1.js:2952

(anonymous function) @ jquery-1.9.1.js:3677

jQuery.extend.each @ jquery-1.9.1.js:648

jQuery.fn.jQuery.each @ jquery-1.9.1.js:270

jQuery.fn.extend.trigger @ jquery-1.9.1.js:3676

jQuery.fn.(anonymous function) @ jquery-1.9.1.js:7403

$.fn.extend.focus @ jquery-ui.js:230

Chosen.results_show @ chosen.jquery.js:968

Chosen.container_mousedown @ chosen.jquery.js:839

(anonymous function) @ chosen.jquery.js:654

jQuery.event.dispatch @ jquery-1.9.1.js:3074

elemData.handle @ jquery-1.9.1.js:2750

我做一個小提琴,這是不一樣的,我p中的設計roject,但錯誤再現完美(請注意第一次打開時選擇的chrome控制檯)。

你可以在這裏查看小提琴:

http://jsfiddle.net/y2h3ohr3/2/

誰能幫助我?我被困住了,因爲我沒有在任何地方發現錯誤,所以我無法做出更多。錯誤是由jquery拋出,而不是選擇,所以我沒有定製appendChild

+0

我可以將更多的信息,只是告訴我,和我分享你需要考慮這個問題。謝謝 –

+1

對於我來說Firefox並不管用。你能說你使用jscrollpane.js嗎?這是不夠的choose.js?如果你從[https://harvesthq.github.io/chosen/](https://harvesthq.github.io/chosen/)上的演示中解釋你的方法不同,我將盡量在晚上實現它。 – vitalii

+0

這是客戶端的一個奇怪的規格。必須使用jscrollpane。是的,這是粗俗的,狗屎的,你所想的所有形容詞都在思考着我。但我無法刪除它。如果你看到小提琴,在選擇的代碼被修改爲使用jscrollpane,所以它不是原來的選擇相同。這是更好的調試一步一步,但我做到了,我不知道問題在哪裏。如果您需要特定信息,請告訴我。謝謝!! –

回答

1

這個問題似乎是在下面的行: pane = $('<div class="jspPane" />').css('padding', originalPadding).append(elem.children());

代替append應該appendTodiv.jsPane被添加到現有的元件。 pane = $('<div class="jspPane" />').css('padding', originalPadding).appendTo(elem.children());

jsFiddle here

+0

控制檯中是否有任何錯誤? – Taleeb

+0

不是現在,錯誤消失了,沒關係。但行爲仍然很奇怪 –

1

這是因爲在你的提琴行115:

pane = $('<div class="jspPane" />').css('padding', originalPadding).append(elem.children()); 

您嘗試追加elem.children()到您的窗格,但是當你選擇在你選擇的東西你的元素變成了孩子。所以jQuery試圖刪除並添加一個不再存在的子元素。 一個快速解決方案是克隆elem.children,將它們存儲在var中並追加克隆的子項。就像這樣:

var $children = elem.children().clone(); 
        pane = $('<div class="jspPane" />').css('padding', originalPadding).append($children); 

Updated fiddle

+0

不幸的是,這只是第一次,我打開選擇。下一次它不起作用......這很奇怪,因爲在小提琴工作正常。在@Taleeb解決方案中也是如此。 –

相關問題