开发者

Jquery: execute function without event

开发者 https://www.devze.com 2023-03-11 08:54 出处:网络
I want to execute some functions on a selec开发者_StackOverflow社区ted element $(selected), using this as the selector.

I want to execute some functions on a selec开发者_StackOverflow社区ted element $(selected), using this as the selector.

$(external).click(function(){
  //relevant code below
  $(selected).????(){
    console.log($(this).html()); //i.e.
    console.log($(this).height()); //i.e.
  }
});

Basically, I don't know what to put for in place of ???? above. Any ideas?

Edits * reworded question, and added sample code for clarity.


If you want to execute a function after the page is loaded with jquery you can do it like

$(function(){
    //code
});

Or if you want it executed right away:

(function(){
    //code
}());


I think you're after .each()

$(external).click(function(){
  //relevant code below
  $(selected).each(function(){
    console.log($(this).html()); //i.e.
    console.log($(this).height()); //i.e.
  });
});

Doc: http://api.jquery.com/each/


Using external as the element:

<div id="external">
    <div id="internal">
        inner
    </div>
</div>

Then for original element:

$(document).ready(function(){
var external = $("#external");
    external.click(function(){
      var selected = $(this);
        console.log(selected.html()); //i.e.
        console.log(selected.height()); //i.e.
      });
});

Or element contained within:

$(document).ready(function(){
var external = $("#external");
    external.click(function(){
      var selected = $(this).find("#internal");
        console.log(selected.html()); //i.e.
        console.log(selected.height()); //i.e.
      });
});
0

精彩评论

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