【问题标题】:Jquery end() methodjQuery end() 方法
【发布时间】:2012-11-09 09:29:32
【问题描述】:

我有一个这样的html文件:

<!DOCTYPE HTML">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<ul id="a">
    <li>list</li>
</ul>
<ul id="b"></ul>

<script type="text/JavaScript" src="js/1.3.2/jquery.min.js"></script>

</body>
</html>

当我加载这个脚本时:

alert(
    jQuery('ul#a li')
    .parent()
    .clone(true)
    .find('li')
    .appendTo('#b')
    .end()
    .end()
    .text();
)

它给了我 1 个空行。但是,如果我这样做:

alert(
    jQuery('ul#a li')
    .parent()
    .clone(true)
    .text();
)

它在警报框上显示“列表”。 我希望上面的两个代码是相同的,所以它们应该显示相同的结果。你能解释一下为什么会出现这种差异吗?

谢谢。

【问题讨论】:

    标签: javascript jquery jquery-end


    【解决方案1】:

    我稍微修改了你的代码

    基本上,你有 1 个 .end() 到多个,这会将太多元素从堆栈中推出

         alert(
            jQuery('#a li')
            .parent()
            .clone(true)
            .find('li')
            .appendTo('#b')
            .end() // If you remove this it will give you the text of the newly added element
            .text() //else it will give you the text of the element you cloned
        )
    

    这样做会得到与上面相同的结果,但没有结尾(显然不会在任何地方附加它 - 但相同的文本)

        alert(
            jQuery('#a li')
            .parent()
            .clone(true)
            .text()
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多