我注意到奇怪的行为。我的页面上有一个隐藏的(显示:“无”)HTML。然后,我创建一个工具提示,并将一些数据从隐藏的 HTML 中提取到此工具提示中,作为示例,如下所示:
$('#tooltip').html( $('#hiddenElement').html() );
如果我修改该隐藏 html 中的类名称(现在位于工具提示内部),则该类名称始终保持原始(不变)当通过 DOM 访问它时:
alert($('#hiddenElement .element').hasClass('some-class');
所以看起来提取 HTML 效果不佳,就像您使用不反映 DOM 的 HTML 副本一样。谁能解释到底发生了什么?我没有测试用例。希望有人熟悉我所描述的内容。谢谢
Strange behavior I noticed. I have a hidden (display:'none') HTML on a page. Then I create a tooltip and extract some data from that hidden HTML into this tooltip, as an example, this way:
$('#tooltip').html( $('#hiddenElement').html() );
If I modify class name within that hidden html (which is now inside of tooltip) that class name always remains original (unchanged) when it is accessed through DOM:
alert($('#hiddenElement .element').hasClass('some-class');
So it looks like extracting HTML does not work well as if you use a copy of it which does not reflect DOM. Can anyone explain what really happens? I do not have a test case. Hopefully someone is familiar with what I describe. Thanks
发布评论
评论(3)
$('#hiddenElement').html()
将#hiddenElement
的innerHtml
作为单个字符串返回。将该字符串插入
$('#tooltip').html( /*here*/ );
将导致 jQuery 检测到您已经给出了一个字符串,因此它将继续并解析该字符串到新的HtmlElement
。这意味着您已经从#hiddenElement
的内容有效地创建了一个克隆,其方式比 jQuery.fn.clone()。如果您不想创建克隆,则可以使用
jQuery.fn.contents()
:但是,由于这不会克隆内容,因此会将其移动到新位置,因此内容将不再位于<代码>#hiddenElement。
据我所知,单个 DOM 节点不可能同时位于两个父节点中。
$('#hiddenElement').html()
returns theinnerHtml
of#hiddenElement
as one single string.Inserting that string into
$('#tooltip').html( /*here*/ );
will cause jQuery to detect that you've given a string, and therefore it will continue and parse the string to newHtmlElement
's. This means you've effectively created a clone from the contents of#hiddenElement
, in a way that costs a lot more time than jQuery.fn.clone().If you do not want to create clones, you could instead use
jQuery.fn.contents()
:However, as this does not clone the contents, it will move them to a new location, and therefore the content will no longer be in the
#hiddenElement
.As far as I know, there is no way for a single DOM-node to be in two parent nodes at the same time.
$('#tooltip').html( $('#hiddenElement').html() );
此行将用替换
的内容,但#tooltip
的内容#hiddenElement#hiddenElement
保持不变。当您修改
#hiddenElement
内的任何内容时,它将仅针对此元素。没有引用前一行中复制的内容,因此当您修改#hiddenElement
的内容时,#tooltip
的内容不会发生变化。如果要将内容从一个元素移动到另一个元素,您应该使用
append
方法,而不是html
方法。$('#tooltip').html( $('#hiddenElement').html() );
this line will replace#tooltip
's content by#hiddenElement
's content but#hiddenElement
remains unchanged.When you modify anything inside
#hiddenElement
it will be just for this element. There is no reference to the content which was copied in the earlier line so there will be no change in#tooltip
's content when you modify#hiddenElement
's content.Instead of
html
method you should useappend
method if you want to move content from one element to another.$('#hiddenElement').html()
获取hiddenElement div 下的HTML 标记,并且hiddenElement div 本身不包含在其中。因此,您可以使用类似
$('#tooltip .element')
来更改类$('#hiddenElement').html()
fetchs the HTML markup under hiddenElement div and the hiddenElement div itself is not included in it.Hence, you can use something like
$('#tooltip .element')
to change the class