简体   繁体   中英

load content from external file javascript

I want to load div which's id equal to id ('#content'+divId) for example button's id=1 and it must load content from file.php which div id equal to content_1 and button's id=2 and it must load content from file.php which div id equal to content_2. both content loads form the same file and it must be shown in different divs.

here is my js

$(document).ready(function() {         
var hash = window.location.hash.substr(1);
var href = $('#nav li a').each(function(){
        var href = $(this).attr('href');
    if(hash==href.substr(0,href.length-4)){

        var toLoad = hash+'.html #content_low'+divId;
        $('#content'+divId).load(toLoad);
        alert(divId);
    }                                           
});

$('#nav li a').click(function(){
    divId = $(this).parents('li').attr('id');   
    var toLoad = $(this).attr('href')+'#content_low'+divId;
    alert(divId);
    $('#content'+divId).slideUp('normal',loadContent).delay(200);
    window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-$(this).attr('href').length);
    function loadContent() {
        $('#content'+divId).load(toLoad,showNewContent)
    }
    function showNewContent() {
        $('#content'+divId).delay(700).slideDown($('#content'+divId).height());

    }
    return false;

});
});

here is html

<ul id="nav" class="marketlist"><li id="1" class="market twobox">
<a href="external_file.php"><img src="img/projects/1.jpg" class="items" alt="" /></a>
</li><li id="2" class="market"> 
<a href="external_file.php"><img src="img/projects/1.jpg" class="items" alt="" /></a>
</li>
</ul>

Correct me if i'm wrong, but your div's dont contain any id. so there will never be a match.

I would generate the div's or LI's by code.

$('#nav').append("<li id='#content'" + divId + "'></li>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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