簡體   English   中英

從外部文件javascript加載內容

[英]load content from external file javascript

我想加載id等於id ('#content'+divId) div,例如按鈕的id=1 ,它必須從file.php加載內容,而div的id等於content_1和按鈕的id=2並且它必須從div ID等於content_2的file.php 兩種內容加載都形成同一個文件,並且必須在不同的div中顯示。

這是我的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;

});
});

這是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>

如果我錯了,請指正我,但您的div不包含任何ID。 所以永遠不會有比賽。

我將通過代碼生成div或LI。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM