简体   繁体   English

使用jQuery在目标div中加载内容

[英]Loading content in a target div using jquery

So the first part of the code works fine as it should be, the two grid and list view loads in the views-div when clicked, however, I want a default view shown in the views-div using jquery, I tried loading using clone and prependto but it doesn't work. 因此,代码的第一部分应该可以正常工作,单击时,两个网格视图和列表视图会加载到views-div中,但是,我希望使用jquery在views-div中显示默认视图,我尝试使用clone加载和prependto,但是它不起作用。 any suggestion on how to do this? 关于如何做到这一点的任何建议?

note: the content I'm loading from the backend has tags and ID's so if I use the html markup to show a default content in the views-div the content repeats. 注意:我从后端加载的内容具有标签和ID,因此,如果我使用html标记在views-div中显示默认内容,则会重复该内容。 So I'm hoping if use jquery to load content, the repeating will not occur. 所以我希望如果使用jquery来加载内容,则不会发生重复。

here's a demo http://jsfiddle.net/soulsurfer/eta0uyye/ 这是一个演示http://jsfiddle.net/soulsurfer/eta0uyye/

$(document).ready(function() {

$('.iw-grid-select, .iw-list-select').on('click', function() {
    var aID = $(this).attr('href');
    var elem = $('' + aID).html();
    $('#iw-grid-view').fadeOut("slow", 1000);
    $('#iw-listview').fadeOut("slow", 1000);
    $('#iw-views-div').html(elem);

});

$( "#iw-grid-view" ).contents().find( ".iw-grid-list-col" ).clone().prependTo("#iw-views-div");
});

The simplest solution could be is to trigger a click event 最简单的解决方案可能是触发点击事件

 $(document).ready(function() { $('.iw-grid-select, .iw-list-select').on('click.view', function() { var aID = $(this).attr('href'); var elem = $('' + aID).html(); $('#iw-grid-view').fadeOut("slow", 1000); $('#iw-listview').fadeOut("slow", 1000); $('#iw-views-div').html(elem); }).first().trigger('click.view'); }); 
 .iw-listview, .iw-grid-view { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="iw-filter-col "> <a href="#iw-grid-view " class="iw-grid-select ">grid view link</a>&nbsp; <a href="#iw-listview " class="iw-list-select ">list view link</a> </div> <div class="row iw-listing-view-row"> <div class="iw-grid-view" id="iw-grid-view">Grid view content</div> <div id="iw-listview" class="iw-listview">list view content</div> <div class="iw-views-div" id="iw-views-div">Content loading column</div> <div id="loading"></div> </div> 

I know that this has been resolved, but just in case anyone was wondering where OP went wrong, I have a theory. 我知道这已经解决了,但是万一有人想知道OP哪里出了问题,我有一个理论。

This: 这个:

$( "#iw-grid-view" ).contents().find( ".iw-grid-list-col" ).clone().prependTo("#iw-views-div");

Should be changed to this: 应更改为:

$( "#iw-grid-view" ).find( ".iw-grid-list-col" ).clone().prependTo("#iw-views-div");

If the .iw-grid-list-col element was an immediate child of #iw-grid-view , then find() wouldn't have found it when called on the return value of contents() . 如果.iw-grid-list-col元素是的直接子#iw-grid-view ,然后find()就不会发现它的返回值时调用contents() This is because find() searches through descendants of elements. 这是因为find()搜索元素的后代 The return value of contents() , in this case, would have included the .iw-grid-list-col element and find() would not have found it since it was a member of the array that find() was called on, rather than a descendant of a member of the array. 在这种情况下, contents()的返回值将包括.iw-grid-list-col元素,而find()不会找到它,因为它是调用find()的数组的成员,而不是数组成员的后代。

Removing contents() from that chain of function calls allows find() to search all of the descendants of #iw-grid-view instead of just the descendants of its immediate children. 从该函数调用链中删除contents()可使find()搜索#iw-grid-view所有后代,而不仅仅是其直接子代的后代。

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

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