var el = $(this); // it's a form
在某一點上:jQuery replaceWith不保留鏈接到DOM中的元素?
el.replaceWith('<p>Loading...</p>');
後來:
el.replaceWith(output);
顯然EL不第一replaceWith後不復存在......
我能留下莫名其妙el
,顯然與新的內容?
var el = $(this); // it's a form
在某一點上:jQuery replaceWith不保留鏈接到DOM中的元素?
el.replaceWith('<p>Loading...</p>');
後來:
el.replaceWith(output);
顯然EL不第一replaceWith後不復存在......
我能留下莫名其妙el
,顯然與新的內容?
原始el
已被刪除並替換爲replaceWith
。創建一個新的參考,使用replaceWith
返回值:
var el = $(this); // it's a form
el = el.replaceWith('<p>Loading...</p>');
//`el.replaceWith()` returns the new element
el = el.replaceWith(output);
如果您打算更換新的元素內的內容,同時保持形狀,用途:
el.html(""); //Clear html
el.append('<p>Loading...</p>');
jQuery的replaceWith返回替換元素,而不是新的
http://api.jquery.com/replaceAll/
你應該做這樣的事情:
var el = $(this); // it's a form
var $p = $('<p>Loading...</p>');
el.replaceWith($p);
// now el is unhooked from the dom, that is el.parent() === []
// it is $p who is now hooked, check with $p.parent() === (some node)
//so you should reassing it
el = $p;
// later on
el.replaceWith(output); // but remenber, once again el is unhooked
也,一定要檢查這個問題:https://stackoverflow.com/a/892915/47633
你可以使用'的.html()'更換元件內部無需更換元素本身。 – Blazemonger