试图在.load触发后注入元素

Trying to inject element after .load fires

本文关键字:注入 元素 load      更新时间:2023-09-26

我试图设置。myshop的内容,从外部xml文件加载后。到目前为止还没有运气。我认为我的时机是正确的,但我也认为一个加载的元素可能不是DOM的一部分?

结果如下:"index.html"加载"template_xml.js"。"template_xml.js"触发一个函数从"common.html"中获取一些常见的页面元素。到目前为止一切都很顺利。然后"template_xml.js"从"index.xml"中加载一些数据,我想用商店名称替换每一个出现的MY SHOP。

我想这是故障代码:

            $(".pagecontent").html($(this).find("pageText").text(), function(){
                $(.shop_name).text("My Great Store Name");
            });

但是它没有将。shop_name的html设置为"My Great Store Name"。感谢所有的帮助。网址:http://chainery.comoj.com

编辑 我将尽量添加相关的代码,而不会让人不知所措。

INDEX.HTML—这是一个非常直接的html文件,就像模板一样。它包含了所有的html元素并加载了javascript。这是" pagcontent "所在的部分

         <!-- Begin Right Column -->
    <div id="content">
        <div class="page_navigation"></div>
        <div id="loading"></div>
        <div class="pagecontent"></div> <!-- USED FOR NON PRODUCT BOXES -->
        <ul class="content"></ul> <!-- USED FOR PRODUCT BOXES -->
        <div style="clear:both; height:10px;"><!--spacer for navigation--></div>
        <div class="page_navigation"></div>
    </div>
     <!-- End Right Column -->

加载template_xml.js-该文件将"common.html"文件中的不同元素加载到index.html中,只是一个文本片段。

COMMON.HTML -此页面是纯html,包含驻留在多个页面上的元素。

<!-- START Navigation -->
    <div id="menu">
        <ul id="navMenu">
            <li id='home'><a href="#index" rel="ajax">Home</a></li>
            <li id='about'><a href="#">About</a></li>
            <li id='info'>
                <a href="#information">Information</a>
                <ul>
                   <li><a href="#metals">Metals</a></li>
                   <li><a href="#sizing">Jewelry Sizing</a></li>
                   <li><a href="#findings">Findings</a></li>
                </ul>
            </li>
            <li id='faqs'><a href="#faqs" rel="ajax">FAQs</a></li>
            <li id='privacy'><a href="#">Privacy</a></li>
            <li id='contact'><a href="#">Contact</a></li>
        </ul>
    </div>

TEMPLATE _XML.JS—该页执行javascript,首先将common.html中的公共元素加载到index.html中。然后加载一个xml文件来填充剩余部分。

下面是加载xml文件并将其放入index.html的函数:
 /* Load up non-product pages */
function get_page_XML(thePage){
//hide the content 
$('.page_navigation').hide();
$('.content').hide();
$('.pagecontent').hide();
$('#loading').show();
/* unhighlight current menu tab */
$('#navMenu li[class="current_page_item"]').attr('class', '');
$.wtMeta('hTab','');
$.get("page_xml/"+thePage+".xml", function(data) {
    /* set the meta tag that identifies this xml */
    $.wtMeta('xmlFile',thePage);
    if(($(data).find(':first-child').attr('type')) == "content"){
        $(".pagecontent").html('');
        $(data).find('Page').each(function () {
            /* set the title */
            $("title").text($(this).find("pageName").text()+" jewelry");
            //document.title = $(this).find("pageName").text();
            /* if the title is photos do something */

            /* set the content */
             $(".pagecontent").html( $(this).find("pageText").text() );
             $(".shop_name").text("My Great Store Name");


            /* set correct menu tab */
             $.wtMeta('hTab',$(this).find("hMenuHighlight").text());
        });
        $(".pagecontent").show();
        hMenuTabs();
    }else{
        /* load product page */
        loadProducts(data,thePage);
    }
    $('#loading').hide();
});
}

谢谢你的时间,托德

如果你缺少引号,就像这样做

$('.shop_name').html("My Great Store Name");

.html()不以这种方式获取函数。你需要这样写:

 $(".pagecontent").html( $(this).find("pageText").text() );
 $(".shop_name").text("My Great Store Name");