开发者

Add node to jQuery selection

开发者 https://www.devze.com 2023-02-14 18:16 出处:网络
I have a function that applies a wrap to different elements (and does some other stuff that is not relevant for this question). I would like the function to return the globality of the wrap element as

I have a function that applies a wrap to different elements (and does some other stuff that is not relevant for this question). I would like the function to return the globality of the wrap element as jQuery selection. I dont want to have to reselect them.

i have tried something like this:

myfunction(){
 return $mythings.each(function(){
  $wrap = $("div.wrap");
  $(this).wrap($wrap);
  //do some other stuff
  return $wrap;
 });
};

but this was returning $mythings.

So i tried this:

myfunction(){
 var $returnOfTheWrap = $();

 $mythings.each(function(){
  $wrap = $("div.wrap");
  $(this).wrap($wrap);
  //some different actions
  return $returnOfTheWrap.push($wrap);
 });

 return $returnOfTheWrap;
};

returns $($(wrap));

Any idea how i 开发者_StackOverflow中文版could solve this? I would like to get $(wrap0, wrap1, wrap2) back


I think you are looking for the map function http://api.jquery.com/jQuery.map/

try

<SCRIPT type=text/javascript>
        $(function(){
            $.each($(".button"),function(i,n)
            {
                alert(i+ ":" + $(n).html());
            });
            var wrapped = myfunction($(".button"));
            //alert(wrapped.length);
            $.each(wrapped,function(i,n)
            {
                alert(i+ ":" + $(n).html());
            });
        });

            function myfunction(mythings){
                return $.map(mythings,function(n,i){
                    $wrap = $("<div></div>");
                    $(n).wrap($wrap);
                     //do some other stuff
                     return $(n).parent();
                 });
            }

    </SCRIPT>

</HEAD>
<BODY>
    <button class="button">a</button>
    <button class="button">b</button>
    <button class="button">c</button>
</BODY>


Maybe you want to try something like this

This returns my listbox options (unedited however) as DOM elements

var existingtitles = $("#existingtitles option").each(function () { return $(this); });

and work this into an array or w/e

0

精彩评论

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