简体   繁体   English

如何使用ajax在文件中加载特定的div / class?

[英]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. 我有一个名为main.html的文件,其中包含几个div,每个div有2个类:1st是他们的特定类2nd是一个名为menu-item的类,用于确定单击时是否触发事件。

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: 我还有一个gallery.html文件,我希望将其加载到#load-here div中的main.html文件中,让我们说它包含这个:

<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 不知怎的,它没有加载到#load-here div中

看看这个: http//api.jquery.com/load/

$( "#b" ).load( "article.html #target" );

There seem to several issues: 似乎有几个问题:

  1. By this: $( "div" ).click(function() ... you are registering the click on all div s available. This might cause strange behaviour. Better use: $("div.menu-item").click( ... 通过这个: $( "div" ).click(function() ...你正在注册所有可用div的点击。这可能会导致奇怪的行为。更好地使用: $("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. 有时this会欺骗你,因为呼叫的背景可能不同。 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 . 我想你想为你的gallery.html提供一个参数,但正如@charlietfl提到的那样,没有.class But, in the DOM there is a .className . 但是,在DOM中有一个.className Better try to use the jQuery $().attr('class') . 最好尝试使用jQuery $().attr('class') Also, the parameter needs a separator: gallery.html?parameter 此外,该参数需要分隔符: 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. closest()将尝试找到最接近的匹配元素,以防点击目标不在div本身,而是一个子img或其他。 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" ) ... 所以,应该使用建议中的条件来找出关于哪个菜单项被点击的更具体的信息: if ( jqTarget.hasClass( "that-specific-item" ) ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM