我有以下 html 树:
<div class="section yellow" id="section1">
<div id="content-wrap1" class="content-wrap">
</div>
我正在使用 ajax 将以下内容加载到#content-wrap1
:
<div id="content">
<a href="whoweare.php">Who we are</a>
<a href="whatwedo.php">Our team</a>
<p>ABOUT US : Dance is a type of art</p>
</div>
成功发生,生成的 HTML 如下所示:
<div class="section yellow" id="section1">
<div id="content-wrap1" class="content-wrap">.
<div id="content">
<a href="whoweare.php">Who we are</a>
<a href="whatwedo.php">Our team</a>
<p>ABOUT US : Dance is a type of art</p>
</div>
</div>
现在我想点击动态加载的链接来加载以下内容:
<div id="content">
<a href="whoweare.php">Who we are</a>
<a href="whatwedo.php">Our team</a>
<p>Hi there! How are you?</p>
</div>
使最终的 HTML 看起来像这样:
<div class="section yellow" id="section1">
<div id="content-wrap1" class="content-wrap">.
<div id="content">
<a href="whoweare.php">Who we are</a>
<a href="whatwedo.php">Our team</a>
<p>Hi there! How are you?</p>
</div>
</div>
我听说我应该使用on()
将 jquery 绑定到动态加载的内容。所以我做了以下事情:
('#section1').on("click", "#section1#content-wrap1#content a", function(){
alert("1");
toLoad = $(this).attr('href')+' #content';
$('content-wrap1').load(toLoad);
}
问题1:这是正确的用法on()
吗?因为当我点击链接时,我没有进入alert 1
屏幕,页面只是导航到whoweare.php
问题2:如果我确实设法收到警报,理论上是否可以点击加载内容的链接来替换最初点击的链接?