-1

this.something各関数の外側でどのように使用しますか

$(function(){

var json = '{ "list": [ { "something": "my string", "stuff": "this" }, { "something": "string of me", "stuff": that } ] }';

$.getJSON(json, function (data) {
    $.each(data.list, function() {
        $( "#someclassthatdoesntmatter" ).append("<div class='somediv'>"+something+"</div>")
        //here this.something is defined
        });
    });
//here I want to alert a 'something' on clicking the 'somediv' which it appended to
//didn't come further than: 
//$(document).on('click', '#somediv',function(){
//    alert(???);
//    });
});

どうすればいいですか?

前もって感謝します。

4

2 に答える 2

0

次のようなことができます:

DOM Ready の場合:

$('#someclassthatdoesntmatter').on('click', '.somediv', function() {
    alert($(this).data('something'));
});

他の場所:

$.getJSON(json, function (data) {
    $.each(data.list, function() {
        $("#someclassthatdoesntmatter").append('<div class="somediv" data-somthing="' + something + '">' + something + '</div>');        
    });
});

理想的には、ループで DOM に追加するべきではありませんが、これでアイデアが得られるはずです。

于 2013-09-17T20:48:18.433 に答える
0
$.getJSON(json, function (data) {
    $.each(data.list, function() {
        UseObject(this);
        });
    });

});

function UseObject(obj)
{
    alert($(obj).something);
}
于 2013-09-17T20:33:11.843 に答える