jQuery replaceWith期间的HierarchyRequestError

时间:2013-05-27 07:30:34

标签: javascript jquery dom domexception

我想浏览id为'imgDiv'的div的所有子元素,并用一个用父锚包装图像创建的新元素替换所有子元素。与<image src=...><a href=...><image ...></a>一样。代码如下:

$(document).ready(function(){
    var elem = $('.imgDiv');
    $(elem).children('.image').each(function(){
            var src = $(this).attr('src');//get the src of a image
            console.log(src);
            var fancyAnchor = $(document.createElement('a')).addClass('fancybox');//create an anchor parent of this image
            //add some attr to meet the need of the lib
            fancyAnchor.attr('data-fancybox-group', 'gallery');
            fancyAnchor.attr('href', src);
            //append this to the anchor
            fancyAnchor.append(this);
            //replace this with the new created anchor <a>
            $(this).replaceWith(fancyAnchor);
        });
  });  

错误很奇怪,我认为这是由每个函数引起的吗?

http://images.craigslist.org/3F53If3J85Nd5Ic5L2d5q499af1a13b10124e.jpg test.js:5
Uncaught Error: HierarchyRequestError: DOM Exception 3 jquery.js:5
x.fn.extend.replaceWith jquery.js:5
x.fn.extend.domManip jquery.js:5
x.fn.extend.replaceWith jquery.js:5
(anonymous function) test.js:13
x.extend.each jquery.js:4
x.fn.x.each jquery.js:4
(anonymous function) test.js:3
x.Callbacks.l jquery.js:4
x.Callbacks.c.fireWith jquery.js:4
x.extend.ready jquery.js:4
S jquery.js:4

如果你想调试,这里是HTML页面的摘录

<body>
<div class="srchDiv"><p class="row" data-pid="3831489360"> <a href="/gbs/cto/3831489360.html" class="i" data-id="3F53If3J85Nd5Ic5L2d5q499af1a13b10124e.jpg"></a> <span class="pl"> <span class="star v" title="save this post in your favorites list"></span>  <a href="/gbs/cto/3831489360.html">1980 DATSUN 210</a> </span> <span class="l2"> <span class="pnr">  <span class="pp"></span> <small> (BOSTON)</small> <span class="px"> <span class="p"> pic</span></span> </span>  </span> </p><span class="msgSpan">Year: 1980</span><div class="imgDiv"><img class="image" src="http://images.craigslist.org/3F53If3J85Nd5Ic5L2d5q499af1a13b10124e.jpg"><img class="image" src="http://images.craigslist.org/3Kc3L53Jc5N45Ea5F7d5q7913e0d4462611cf.jpg"><img class="image" src="http://images.craigslist.org/3G23Ld3Jf5I95G45K8d5qe8c553cf8b961548.jpg"><img class="image" src="http://images.craigslist.org/3Kf3F73N85I25Gc5E7d5q80635196e0161ab7.jpg"><br><img class="image" src="http://images.craigslist.org/3E73Ge3L55L85Kb5H3d5qdf21ef9bd8c31c0d.jpg"><img class="image" src="http://images.craigslist.org/3Ec3G13J95Lf5K45M6d5q917b9e39d65217bd.jpg"><img class="image" src="http://images.craigslist.org/3Ff3L83Hd5L75I75E8d5q9a2b1c55c74710b5.jpg"><img class="image" src="http://images.craigslist.org/3Ef3Gb3H35La5I65H3d5q57b390b1f8ce1389.jpg"></div></div>
</body>

2 个答案:

答案 0 :(得分:3)

你试图用附加它的元素替换this,并通过附加一个元素,实际上移动它,因此,在这里,将它从DOM中删除,因此错误

替换

fancyAnchor.append(this);
$(this).replaceWith(fancyAnchor);

$(this).wrap(fancyAnchor);

答案 1 :(得分:-1)

不要使用$(this).replace或更改每个内部的DOM。

编辑:似乎不是问题

&GT;

我还建议使用$ .each(...,f)代替$(...)。每个(f)。

你也应该考虑wrap function而不是$ .each,它完全符合你的需要:

$(elem).children('.image').wrap(function(){
            return $('<a>').addClass('fancybox').attr('data-fancybox-group', 'gallery').attr('href', $(this).attr('src'));
        });