开发者

JQuery each() failure loops

开发者 https://www.devze.com 2023-03-27 00:53 出处:网络
I try to write a loop to wrap all img on the picture via Jquery: var $csstring = \"div.article_column a img\";

I try to write a loop to wrap all img on the picture via Jquery:

var $csstring = "div.article_column a img";
$($csstring).each(
  function() {    
        $($csstring).parent().wrap("<div class='dropshadow'></div>");
 });  

and I receive this:

<div class="dropshadow">
    <div class="dropshadow">
        <div class="dr开发者_如何学运维opshadow">
            <div class="dropshadow">>
                <div class="dropshadow">
                    <a target="_blank" href="/ferdynand.jpg">
                        <img height="126" width="190" border="0" src="aae7dda0d83bf7df21ce6f834db8228a.jpg">
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

I mean that was 5 pictures with the same css, Can somebody help me?


You can do this without looping

// this contains an array of elements
var $csstring = $("div.article_column a img"); 

// this will wrap all objects with your html
$csstring.wrap("<div class='dropshadow'></div>"); 

Note: You need to wrap your string with $() to use the variable as a jquery object.

Edit: If you want to access a specific node in the array...

var $firstElement = $($csstring[0]);


Change $csstring to this within the loop function:

var $csstring = "div.article_column a img";
$($csstring).each(
    function() {    
        $(this).parent().wrap("<div class='dropshadow'></div>");
    });


Do this instead:

$($csstring).each(function() {
    $(this).parent().wrap('<div class="dropshadow"></div>');
});


I don't think you even need each. Just do this:

$("div.article_column a img").parent().wrap("<div class='dropshadow'></div>");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号