Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

298
Visualizações
How to load a specific div/class in a file using ajax?

I have a file called main.html that contains several div's each with 2 classes: 1st is their specific class 2nd is a class called menu-item which I use to determine if an event will triggered when clicked.

The file contains this:

<div id="load-here">

</div>

<div class="item-1 menu-item">
    click this
</div>

I also have a gallery.html file which I want to be loaded into the main.html file in the #load-here div, and let's say it contains this:

<div class="menu-item>
    <!-- some image here -->
    <img href="img/1.jpg />
</div>

The script I have is this:

$(document).ready(function() {
    $( "div" ).click(function() {

        if ( $( this ).hasClass( "menu-item" ) ) {
            $("#load-here").load("gallery.html" + this.class);
        }

    });
});

The problem: It's not working. I've tried various changes. Somehow it's not loading into the #load-here div

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Take a look at this: http://api.jquery.com/load/

$( "#b" ).load( "article.html #target" );
over 4 years ago · Santiago Trujillo Relatório

0

There seem to several issues:

  1. By this: $( "div" ).click(function() ... you are registering the click on all divs available. This might cause strange behaviour. Better use: $("div.menu-item").click( ...
  2. Make sure you understand the context and the event target in your callback function. Sometimes this is cheating you, as the context of the call may be different. Things are more clear if you handle the event as explicit parameter and check the event target.
  3. I guess you would like to give a parameter for your gallery.html, but as @charlietfl mentions, there is no .class. But, in the DOM there is a .className. Better try to use the jQuery $().attr('class'). Also, the parameter needs a separator: gallery.html?parameter

To sum it up, here my suggestion:

$("div.menu-item").click( function(event) { 

  var jqTarget = $(event.target).closest('.menu-item');

  if ( jqTarget.hasClass( "menu-item" ) ) {
    $("#load-here").load("gallery.html" + "?" + this.class);
  }

});

closest() will attempt to find the closest matching element, in case the click target was not on the div itself, but rather a child img or else. So, the condition in the suggestion should be used to find out about more specific about which menu item was clicked: if ( jqTarget.hasClass( "that-specific-item" ) ...

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda