简体   繁体   English

动态加载的内容点击事件未触发

[英]dynamically loaded content click event not firing

Currently, I am dynamically loading HTML via a dropdown click event (via jquery event) The problem that arises is my .on event function is not finding the selector to fire the "a" tag click event. 当前,我正在通过下拉单击事件(通过jquery事件)动态加载HTML。出现的问题是我的.on事件函数未找到触发“ a”标签单击事件的选择器。 If the original page that is loaded contains html with the "a" tag with the specified class, the click event for that "a" tag works fine, BUT if I dynamically load new content via the jquery event, then IF a clickable "a" tag exists and I click it, the original page just posts back, there is no evidence of the "a" tags click event firing. 如果加载的原始页面包含带有带有指定类的“ a”标签的html,则该“ a”标签的click事件可以正常工作,但是如果我通过jquery事件动态加载新内容,则如果可点击的“ a”标签存在,然后单击它,原始页面刚刚发回,没有证据表明“ a”标签click事件触发。

Here is a sample of the dynamically loaded content; 这是动态加载内容的样本;

<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&nbsp;<a ID="creamcheesefrosting.txt" class="icing" 
            href="#">here</a>&nbsp; for frosting recipe</p>
    </div>
    <div id="recipeimage">
        <img src="Images/RecipeImages/carrotcake_160x233.png" alt=""/>
    </div>
</div>

Here is how the content is loaded, the html is retrieved via a WCF service which I've confirmed is working; 这是内容的加载方式,通过WCF服务检索html,我已经确认它可以正常工作;

function resetRecipeHtml(html, recipename) {
$("#lblRecipeLegend").text("Recipe for " + recipename);
$("#recipecard").html(html);

} }

Here is the jquery event code, for now this code is just in a script block on the page outside the document ready code ( doesn't work if it's in it either) Note: The alert is there only to prove the event is firing.; 这是jquery事件代码,目前此代码仅位于文档就绪代码之外的页面上的脚本块中(如果其中包含也无效)注:出现警报仅是为了证明事件正在触发。 ;

 $('#recipedirections p').on('click', 'a.icing', function (e) {
      e.preventDefault();
      var filename = $(this).attr("id");
      alert(filename);
      getRecipeHtml(filename);          
  });

Part of your question 您的问题的一部分

Here is a sample of the dynamically loaded content; 这是动态加载内容的样本;

<div id="recipedirectionsandimage">
    <div id="recipedirections">

As, recipedirections is also a part of dynamically loaded content. 如此, recipedirections也是动态加载内容的一部分。 You should use document or nearest static container. 您应该使用document或最近的静态容器。 You should use recipecard 您应该使用recipecard

 $("#recipecard").on('click', 'a.icing', function (e) {
      e.preventDefault();
      var filename = $(this).attr("id");
      alert(filename);
      getRecipeHtml(filename);          
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM