从一个HTML导入文本并使用javascript将其插入另一个HTML

时间:2014-04-10 23:02:57

标签: javascript php jquery html

我试图在我设计的网站主页上制作最新的新闻样式部分,我希望从我的文章页面中获取标题并将其用作我的标题我的主页上的最新新闻部分,我主页上的最新新闻部分如下所示:

<body onload="insertText()">
<section class="latest">
                <h1 class="latesth" id="test"> Latest News: </h1>

                        <section class="title" onclick="changeImagea()">
                            <h3 id="h1" > Mcilroy Wins US Open </h3>
                            <p> He wins by three shots in horrendous condition, <u>click here</u> to read more </p>
                        </section>

                        <section class="title" onclick="changeImageb()">
                            <h3> Tiger Wins Masters </h3>
                            <p> He wins by three shots in horrendous condition, <u>click here</u> to read more </p>
                        </section>

                        <section class="titlebottom" onclick="changeImagec()">
                            <h3> Mcilroy Wins US Open </h3>
                            <p> He wins by three shots in horrendous condition, <u>click here</u> to read more </p>
                        </section>
                </section>
到目前为止,我执行此操作的javascript看起来像这样,并且在外部javascript文件中:

 function insertText () {
    var x = document.getElementById('ahead').innerHTML;
    document.getElementById('h1').innerHTML = x;
    }

最后在article.php页面id中的文本就像抓住并插入主页看起来像这样:

<section>
<h1 id="ahead"> hello </h1>
</section>

感谢您的帮助,希望我很清楚。

1 个答案:

答案 0 :(得分:0)

我建议使用jQuery,它可以节省大量的时间和问题。

如果#ahead元素在另一个页面中,您可以执行以下操作:

$('h1').load('/foo #ahead');