0

鉴于我在 javascript 变量“数据”中有这个 html 字符串

<div class="packery-item">        
        <img class="rsImg" src="/media/VERSIONS/images/blog/2013/04/heart-watercolor1_featured_image.jpg" alt="Article illustration"/>        
</div>
<div class="packery-item">        
        <img class="rsImg" src="/media/VERSIONS/images/blog/2013/04/planning_featured_image.jpg" alt="Article illustration"/>        
</div>
<div class="packery-item">
    <img src="/media/VERSIONS/images/galleries/2013/04/fashion_slideshow.jpg" alt="" />
</div>

当我运行$(data)它返回类似:

[div.packery-item, text, div.packery-item, text, div.packery-item, text, div.packery-item, text, div.packery-item, text, div.packery-item, constructor: function, init: function, selector: "", jquery: "1.8.3", size: function…]

我希望它只返回一个 div.packery-item 列表。我做错了什么?

4

1 回答 1

0

您需要摆脱标签之间的空白,它们被转换为文本节点,以便元素之间有间距。

var data = '<div class="packery-item">' +
        '<img class="rsImg" src="/media/VERSIONS/images/blog/2013/04/heart-watercolor1_featured_image.jpg" alt="Article illustration"/>' +
'</div>' +
'<div class="packery-item">' +
        '<img class="rsImg" src="/media/VERSIONS/images/blog/2013/04/planning_featured_image.jpg" alt="Article illustration"/>' +
'</div>' +
'<div class="packery-item">' +
    '<img src="/media/VERSIONS/images/galleries/2013/04/fashion_slideshow.jpg" alt="" />' +
'</div>'
于 2013-05-02T03:23:44.127 回答