Javascript 动态加载的内容单击事件未触发
目前,我正在通过下拉单击事件(通过jquery事件)动态加载HTML 出现的问题是my.on事件函数没有找到触发“a”标记单击事件的选择器。 如果加载的原始页面包含带有指定类的“a”标记的html,则该“a”标记的单击事件可以正常工作,但如果我通过jquery事件动态加载新内容,则如果存在可单击的“a”标记并单击它,则原始页面只会发回,没有“a”标记单击事件触发的证据 下面是一个动态加载内容的示例Javascript 动态加载的内容单击事件未触发,javascript,jquery,events,Javascript,Jquery,Events,目前,我正在通过下拉单击事件(通过jquery事件)动态加载HTML 出现的问题是my.on事件函数没有找到触发“a”标记单击事件的选择器。 如果加载的原始页面包含带有指定类的“a”标记的html,则该“a”标记的单击事件可以正常工作,但如果我通过jquery事件动态加载新内容,则如果存在可单击的“a”标记并单击它,则原始页面只会发回,没有“a”标记单击事件触发的证据 下面是一个动态加载内容的示例 <div id="recipedirectionsandimage"> <
<div id="recipedirectionsandimage">
<div id="recipedirections">
<h2 class="recipedirectionsheader"> Recipe Directions</h2>
<ol class="recipesteps">
<li>Pre-Heat oven to 350°, Grease two 9" springform pans</li>
<li>Sift all dry ingredients into a large bowl</li>
<li>Add corn oil, eggs and vanilla, mix well</li>
<li>Fold in walnuts, coconut, carrots and pineapple</li>
<li>Pour batter into the greased pans</li>
<li>Bake for 50 minutes on the middle racks</li>
</ol>
<p>Click <a ID="creamcheesefrosting.txt" class="icing"
href="#">here</a> for frosting recipe</p>
</div>
<div id="recipeimage">
<img src="Images/RecipeImages/carrotcake_160x233.png" alt=""/>
</div>
</div>
<div id="recipedirectionsandimage">
<div id="recipedirections">
}
这是jquery事件代码,目前此代码仅位于页面上文档就绪代码之外的脚本块中(如果在其中也不起作用)注意:警报仅用于证明事件正在触发
$('#recipedirections p').on('click', 'a.icing', function (e) {
e.preventDefault();
var filename = $(this).attr("id");
alert(filename);
getRecipeHtml(filename);
});
你问题的一部分 下面是一个动态加载内容的示例
<div id="recipedirectionsandimage">
<div id="recipedirections">
<h2 class="recipedirectionsheader"> Recipe Directions</h2>
<ol class="recipesteps">
<li>Pre-Heat oven to 350°, Grease two 9" springform pans</li>
<li>Sift all dry ingredients into a large bowl</li>
<li>Add corn oil, eggs and vanilla, mix well</li>
<li>Fold in walnuts, coconut, carrots and pineapple</li>
<li>Pour batter into the greased pans</li>
<li>Bake for 50 minutes on the middle racks</li>
</ol>
<p>Click <a ID="creamcheesefrosting.txt" class="icing"
href="#">here</a> for frosting recipe</p>
</div>
<div id="recipeimage">
<img src="Images/RecipeImages/carrotcake_160x233.png" alt=""/>
</div>
</div>
<div id="recipedirectionsandimage">
<div id="recipedirections">
你问题的一部分 下面是一个动态加载内容的示例
<div id="recipedirectionsandimage">
<div id="recipedirections">
<h2 class="recipedirectionsheader"> Recipe Directions</h2>
<ol class="recipesteps">
<li>Pre-Heat oven to 350°, Grease two 9" springform pans</li>
<li>Sift all dry ingredients into a large bowl</li>
<li>Add corn oil, eggs and vanilla, mix well</li>
<li>Fold in walnuts, coconut, carrots and pineapple</li>
<li>Pour batter into the greased pans</li>
<li>Bake for 50 minutes on the middle racks</li>
</ol>
<p>Click <a ID="creamcheesefrosting.txt" class="icing"
href="#">here</a> for frosting recipe</p>
</div>
<div id="recipeimage">
<img src="Images/RecipeImages/carrotcake_160x233.png" alt=""/>
</div>
</div>
<div id="recipedirectionsandimage">
<div id="recipedirections">
你问题的一部分 下面是一个动态加载内容的示例
<div id="recipedirectionsandimage">
<div id="recipedirections">
<h2 class="recipedirectionsheader"> Recipe Directions</h2>
<ol class="recipesteps">
<li>Pre-Heat oven to 350°, Grease two 9" springform pans</li>
<li>Sift all dry ingredients into a large bowl</li>
<li>Add corn oil, eggs and vanilla, mix well</li>
<li>Fold in walnuts, coconut, carrots and pineapple</li>
<li>Pour batter into the greased pans</li>
<li>Bake for 50 minutes on the middle racks</li>
</ol>
<p>Click <a ID="creamcheesefrosting.txt" class="icing"
href="#">here</a> for frosting recipe</p>
</div>
<div id="recipeimage">
<img src="Images/RecipeImages/carrotcake_160x233.png" alt=""/>
</div>
</div>
<div id="recipedirectionsandimage">
<div id="recipedirections">
你问题的一部分 下面是一个动态加载内容的示例
<div id="recipedirectionsandimage">
<div id="recipedirections">
<h2 class="recipedirectionsheader"> Recipe Directions</h2>
<ol class="recipesteps">
<li>Pre-Heat oven to 350°, Grease two 9" springform pans</li>
<li>Sift all dry ingredients into a large bowl</li>
<li>Add corn oil, eggs and vanilla, mix well</li>
<li>Fold in walnuts, coconut, carrots and pineapple</li>
<li>Pour batter into the greased pans</li>
<li>Bake for 50 minutes on the middle racks</li>
</ol>
<p>Click <a ID="creamcheesefrosting.txt" class="icing"
href="#">here</a> for frosting recipe</p>
</div>
<div id="recipeimage">
<img src="Images/RecipeImages/carrotcake_160x233.png" alt=""/>
</div>
</div>
<div id="recipedirectionsandimage">
<div id="recipedirections">
我尝试将其作为测试,但最初没有,现在也没有。请显示动态创建内容的代码,段落当然不会动态插入,
#recipedidirections
,因为您委托的父级必须是静态的。没有必要。我错过了“这是一个动态加载内容的示例”。其他人回答了Deneo…正确,我必须从插入的内容中找到一位家长,现在它被激活了,谢谢。将其作为答案发布,我将接受它。我尝试将其作为测试,但最初没有,现在也没有。请显示动态创建内容的代码,段落当然不会动态插入,#recipedidirections
,因为您委托的家长必须是静态的。没有必要。我错过了“这是一个动态加载内容的示例”。其他人回答了Deneo…正确,我必须从插入的内容中找到一位家长,现在它被激活了,谢谢。将其作为答案发布,我将接受它。我尝试将其作为测试,但最初没有,现在也没有。请显示动态创建内容的代码,段落当然不会动态插入,#recipedidirections
,因为您委托的家长必须是静态的。没有必要。我错过了“这是一个动态加载内容的示例”。其他人回答了Deneo…正确,我必须从插入的内容中找到一位家长,现在它被激活了,谢谢。将其作为答案发布,我将接受它。我尝试将其作为测试,但最初没有,现在也没有。请显示动态创建内容的代码,段落当然不会动态插入,#recipedidirections
,因为您委托的家长必须是静态的。没有必要。我错过了“这是一个动态加载内容的示例”。其他人回答了Deneo…正确,我必须从插入的内容中找到一位家长,现在它被激活了,谢谢。把它作为答案贴出来,我会接受的