问题
Here is the jsfiddel:
class Overlay
constructor: ->
@header = $("<div> header</div>")
@footer = $("<div> footer</div>")
get: ->
@popup = $("<div></div>").append(@header).append(@footer)
@popup.clone(true)
overlay = new Overlay
overlay_extend = {}
$.extend(true,overlay_extend,overlay)
overlay_extend.header.append("<div>more header</div>")
overlay.header.appendTo("body")
In the jsfiddle I changed the extended dom element and the orginal dom is changed..Any new ideas.
回答1:
extend
only clones native JS objects, not DOM elements. Here, the cloned jQuery object still points to the original DOM element.
You need to .clone() the DOM elements.
来源:https://stackoverflow.com/questions/9639836/jquery-gurus-jquery-extend-is-not-cloning-the-dom-elements