简体   繁体   English

动态添加元素时无法触发jQuery事件

[英]JQuery events not firing when adding elements dynamically

I am using http://jaukia.github.io/zoomooz/ to zoom elements on my web page. 我正在使用http://jaukia.github.io/zoomooz/来缩放网页上的元素。

However when I am adding elements dynamically through .append() , the elements don't zoom. 但是,当我通过.append()动态添加元素时,这些元素不会缩放。

I tired using .on and doing stuff manually with no success. 我厌倦了使用.on并手动进行操作,但没有成功。

$(document).on("click", ".lot", function () {
                $(this).zoomTarget();
            });

This is my HTML 这是我的HTML

 <div class="zoomViewport">
            <div class="lotmap zoomContainer">
 </div></div>

Any elements added in the div class="zoomViewport" with data attribute data-targetsize="0.05" and class zoomTarget would be zoomed when clicked. div class="zoomViewport"将在div class="zoomViewport"添加的任何具有数据属性data-targetsize="0.05"zoomTarget类的zoomTarget都将进行缩放。

Example: <img class="zoomTarget lot" data-targetsize="0.05" src="../images/sold.png"/> 示例: <img class="zoomTarget lot" data-targetsize="0.05" src="../images/sold.png"/>

I am appending elements to lotmap . 我将元素添加到lotmap Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: The event is firing but I need the plugin events to work as it used to before adding elements dynamically. 编辑:该事件正在触发,但是我需要插件事件像以前一样工作,然后才能动态添加元素。

I was having the same issue with zoomooz. 我在zoomooz上遇到了同样的问题。 In this example you make new elements by clicking the button. 在此示例中,您可以通过单击按钮来制作新元素。 After the new element is made, you just call zoomooz on it with zoomTarget(): 制作新元素后,您只需使用zoomTarget()对其调用zoomooz:

    <button> Make A Box </button>

    <div class="zoomViewport">
        <div id="container" class="zoomContainer">
            <div class="box zoomTarget"> </div>
        </div>
    </div>

    <script>

        function _calls_zoom(){
            $(".box").zoomTarget();
        }

        $("button").click(function(){
            $("#container").append("<div class='box'></div>");
            _calls_zoom();
        });

    </script>

Not a perfect example, but I uploaded a working copy on my server which includes a bunch of my notes on this as well as a download link so you can grab the example and tweak it to your needs. 这不是一个完美的示例,但是我在服务器上上传了一份工作副本 ,其中包括一堆关于此的注释以及一个下载链接,因此您可以抓住该示例并根据需要进行调整。

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

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