jQuery .wrap()无法正常工作 [英] jQuery .wrap() isn't working

查看:94
本文介绍了jQuery .wrap()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能做错了事,但是我尝试了各种事情,但似乎无法包装到jQuery对象的集合中.以下只是输出未包装的链接HTML.有什么想法吗?

I'm probably doing something wrong but I've tried all sorts of things and can't seem to get a collection of jQuery objects wrapped. The following just outputs the link HTML, unwrapped. Any ideas?

$.each(sitemapSections, function(i) {
  var $sitemapSection = $(sitemapSections[i]);
  var $primary = $sitemapSection.find('a[data-level="1"]').wrap('<h3></h3>');

  $dropdownSections[i].html($primary);
});

编辑-这是标记(清理后):

EDIT - here's the markup (cleaned up):

<li id="product-solutions"><a href="#link" class="alpha grid-6">Products &amp; Solutions</a>

  <div id="ps-dropdown" class="dropdown-menu grid-20">
    <div class="ps-dropdown-section">

    </div><!-- .ps-dropdown-section -->

    <div class="ps-dropdown-section">

    </div><!-- .ps-dropdown-section -->

    <div class="ps-dropdown-section">

    </div><!-- .ps-dropdown-section -->
  </div><!-- .dropdown-menu -->
</li>

更新-我明白了!我缺少的是提到parent()的评论.这是最终的代码:

UPDATE - I got it! The comments who mentioned parent() is what I was missing. Here's the final code:

$.each(sitemapSections, function(i) {
  var $sitemapSection = $(sitemapSections[i]);
  var $primary = $sitemapSection.find('a[data-level="1"]').wrap('<h3></h3>').parent();

  $dropdownSections[i].html($primary);
});

推荐答案

jquery .wrap()将INNER元素返回给您,因此看起来什么都没有改变.

jquery .wrap() returns the INNER element back to you, so it looks like nothing changed.

$("<div>").wrap('<span>') 

===> 

[<div></div>]

看起来不起作用.

但是:

$("<div>").wrap('<span>').parent()

===>

[<span><div></div></span>]

它确实起作用了.

这篇关于jQuery .wrap()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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