简体   繁体   中英

Can't request data from div after ajax call. How do I fix this?

This example works well and the same I use on my project, but the data I use, in this case the div with the data-name attr is loaded by ajax and it looks like it doesn't work. In the console, firefox output this after the click :

getPreventDefault() sollte nicht mehr verwendet werden. Verwenden Sie stattdessen defaultPrevented.jquery.min...

http://jsfiddle.net/n495c/14/

js

      $(document).ready(function() {
        $("#content div").click(function() {
            var title = $(this).data("name");
            $("#content div").text(title);
        });
    });

html

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="content">
<div data-name="Neu"></div>
</div>

css

#content {
    width: 100%;
    height: 100%;
}

#content div {
    width: 50px;
    height: 50px;
    background: red;
    color: white;
}

Delegated event handler :

$(document).ready(function() {
    $(document).on('click', '#content div', function() {
        var title = $(this).data("name");
        $("#content div").text(title);
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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