使用 wrap() 后尝试 append() 时出现奇怪的 jquery 错误 [英] Wierd jquery bug, when trying to append() after using wrap()

查看:22
本文介绍了使用 wrap() 后尝试 append() 时出现奇怪的 jquery 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Using jQuery, I

  1. Created a container

  2. Wrapped an element with it

  3. Selected the container by passing an object to its jQuery selector and tried to add an element to the container

Why doesn't element append()?

Look at this example here: jsfiddle.net/Tngh4

    var container = document.createElement("div");
    //Wrapping some random element with the container
    $("#randomElem").wrap(container);

    var elem = document.createElement("div");
    //Selecting element this way and using append results in a strange bug
    $(container).append(elem);

解决方案

.wrap wraps the element with a copy of the passed in element, not the passed in element itself.

A copy of this structure will be wrapped around each of the elements in the set of matched elements. This method returns the original set of elements for chaining purposes.

http://api.jquery.com/wrap

Here's your fiddle to use an alternative to .wrap http://jsfiddle.net/Tngh4/1/

这篇关于使用 wrap() 后尝试 append() 时出现奇怪的 jquery 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆