gpt4 book ai didi

javascript - Javascript 和 jQuery 的变量

转载 作者:行者123 更新时间:2023-11-28 20:39:00 26 4
gpt4 key购买 nike

请解释一下以下 3 行代码中的 head var 发生了什么(以及使用),而不是摸索着(以我有限的理解)。

<script type="text/javascript">
var head = $("thead#tHead1").clone().removeAttr("class");
$(head).find("#hRow2").remove();
head = $(head).wrap("<thead>").parent().html();

编辑在第一行设置 head = 然后将 head 分配给另一个值之后,在第二行代码中使用 $(head) 有何意义?

查看我所学到的答案。

最佳答案

// Find an element, make a copy of it, and remove it's class
var head = $("thead#tHead1").clone().removeAttr("class");

// Within the cloned element (not on the DOM yet),
// Find an element within and then remove that found element.
$(head).find("#hRow2").remove();

// Wrap the cloned element in another element, giving it a parent.
// Traverse to that parent and return it's html content
head = $(head).wrap("<thead>").parent().html();

请注意,此处不需要 $(head)。可以简单地用 head 来代替。您不需要将元素转换为 jQuery 包装的对象,因为它应该已经是一个。

<小时/>

此外,这是糟糕的代码。这是对 DOM 的一种拙劣的黑客攻击,可以做任何它应该做的事情。相反,您应该使用某种模板引擎根据某些输入数据根据需要生成新的 DOM 片段。

关于javascript - Javascript 和 jQuery 的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14761460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com