Problem to attack son and doubt in this use

Asked

Viewed 79 times

0

I’m having trouble understanding the use of this.

I have the following structure:

<div id="a" class="clsa">
    bbb
</div>

<div id="b" class="clsb">
    <span id="bb" clsbb> Conteudo bb </span>
</div>

I wanted to attack any of the elements at the time of reading it. I am doing this way and this working:

$("#a").ready(function() {
    //Chamando uma função e passando parametros funcionar normalmente, eu queria poder atacar daqui de dentro este mesmo elemento "#a".
})

Now I wish I could take the span of b following the same idea of the previous one and attack this span:

$("#b").ready(function() {
    //Chamar o span e atacar ele (fazer alguma coisa, mudar o texto, colocar uma classe)
})

I’m trying with this, but it’s not rolling. It’s calling Document and not this element:

$("#b").ready(function() {
    this.text("teste");
})

I’m wearing the right one?

I’m making the call "$("#a"). ready(.." right?

I’m making a mistake somewhere?

If it was to attack it after q the page was loaded just change the ready to load equal $(document). ready and (window). load?

Help me with

  • 4

    Can you explain what you mean by atacar? Also explain what you want to do with this text so we can help better.

  • 1

    tried with $(this) instead of this?

  • 1

    It seems to me that attacking means manipulating.

  • In which country is this expression used atacar?

3 answers

2


When you use this for the element that triggered the event, you will need to use it as a jquery selector:

$("#a").ready(function(){
      $(this).html("Oii");
      //Onde $(this) = $("#a");
});

This rule changes when Voce is inside repeat loops, for example, when $(this) will reference the reading element itself:

$("#a").ready(function(){
      $.each($("#minhaUl li"), function(index){
            $(this).html("Oii");
            //Onde $(this) = elemento li em leitura;
      });
});

Which means that this has different value, according to the execution context.

http://www.digital-web.com/articles/scope_in_javascript/

To manipulate the inner elements to the listener of the event, has a number of ways to do.

$("#b").ready(function() {
    $("#b > span").html("Oii");
    $("#b span").html("Oii");
    $("#b").find("span").html("Oii"); //$(this).find("span").html("Oii");
    ... //e outras
})

https://api.jquery.com/child-selector/

https://api.jquery.com/children/

https://api.jquery.com/find/

https://api.jquery.com/nth-child-selector/

But one thing I don’t understand is why are you using $(element).ready(...), pq if you just want to assign a value or something like that, you could use:

$(document).ready(function(){ $("#b").find("span").html("Oii"); ... });

  • What’s the difference in using: $(element). ready(...) e $(Document). ready(Function()' $("#b"). find("span"). html("Oii"); ... });

  • One last question. If I have a function X and want to call it in the element is possible ( $("#b"). x(asdf) )?

  • 1

    Well, I would put inside the Document.ready because: it gets better to read and because the browser will trigger a single ready event and you will run td inside it. Instead of several other events they all do the same thing. Considering that everything inside is only going to run once, it would be better to have a single ready function, wouldn’t it? What about the question about the method. If you want to call qndo executing something, eg: click a button: $("#btn"). click(Function() { method();});

  • As for the ready, thank you very much. It helped me a lot. Regarding the function, it will now work this way q you just explained to me (Document)... Plus and if I haven’t been using (Document)... plus yes $("#b"). x(asdf), it will not be fired via click, mouse, ... but rather when the element is read $("#b"). ready(Function() {...}) . Let’s say I created a function with an action x, wanted to be able to call this function, is it possible? I have the way I passed, only q this giving error, says that there is no function x.

  • 1

    I do not understand well what you mean... When the element is read will happen what?

  • I want to shoot it without being through click, mouse, ... For example, "$("p"). click(Function(){ $(this).Hide(); }); " if clicking will perform the native Hide function of jquery, is there any way I can run it through event click, mouse,..., but when the file is read by the browser? And in this case, I wish I could perform a function that I created " $("p"). x(); "

  • 1

    Of course, in $(Document). ready(Function()' x(); }); This method executes q?

  • 1

    What do you want to do? Explain it right. You want when you open the page to hide something, that’s it?

  • 1

    Blocked chat here. (Y) Blocked images... aheuhas all I can help is just seeing what you have to show.

  • making $(Document). ready(Function(){ x(); }); works and is perfect. Now for the sake of doubt, in this format: " Function funcA(parameter) { var qualquercoisa = parameter; console.info(whatever); } " " $("#b"). ready(Function(){ var conteudo = $(this).html(); funcA("content"); }); " has it working? I separated each instruction with "" to be able to distinguish one code from the other

  • 1

    $(Document). ready(Function(){ funcA($("#b"). html(); }); It will work like this. (=

  • It worked, vlw. Just involve everything by the function?!

  • 1

    Everything you would do: $(element). ready, put into the $(Document). ready(.. ) and it will go out running. $(Document). ready(Function() ? run($("#b"). html()); funcB(); x(); y($("#txt"). val()); } ...

  • Only one $(Document). ready(..) per file neh? This is the last question. Thanking for everything.

  • It is ideal. Nothing prevents you from declaring several times, but it is not nice in terms of organization. (= Arrange.

Show 11 more comments

1

With Jquery you can use child selector ( > ) to reach the span from inside the div #b.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script>
$("#b").ready(function() {
    $("#b > span").html("teste");
})
</script>

<div id="b" class="clsb">
    <span id="bb"> Conteudo bb </span>
    <br>outra coisa q vai continuar aqui 
</div>

1

The method $(Document).ready() will be called at the time of page construction, just this, if you have any posts with ajax for example in your code, after the call of this method the $(Document).ready() will not be called. The second point is the use of this following example:

$("#b").ready(function() {
    // this nesse caso é o objeto #b
    this.text("teste");
})

The this will always be the object that called the method, if you want to use it in $(Document).ready() there is no need to use this way above, example:

$(document).ready(function() {
   $("#bb").text("teste");
})

The use you gave example above, is more in cases of sweeping some list or something similar, example:

// varre todas as divs da página setando nas spans que estão dentro das divs

$("div").each(function()
{
// this nesse caso é a div atual
  $(this).find("span").css("color","red");
})

$("#bb").click(function()
{
// nesse caso o this é a span #bb
$(this).text("teste");
})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.