0

ユーザーが要素をクリックしたときに、要素のタグ名が内部でクリックされるようにしようとしてliいます。要素はliHTMLコードに動的に追加されます。これを行うために、私は以下のコードを使用していますが、機能していないようです。

$('li *','div#contentWrapper').on('click','#interfaceContainer #left #sortableTestComponents li',function(e){
        e.stopPropagation();
        var domEl = $(this).get(0);
        alert(domEl.tagName);
        if(!$(event.target).hasClass("deleteTestComp")){
            var type = $(this).find('div.headerName').html();
            var information = $(this).find('input[type=hidden]').val();

            if(type == "CliSessionType"){
                 parseCliComponent(information);
            }
            else if(type == "DBSessionType"){
                parseDbComponent(information);
            }
            else{
                parseScreenComponent(information);
            }
        }
    });

コードが機能しないのはなぜですか?ユーザーが要素をクリックしても何も起こりません。

JSFiddle- http: //jsfiddle.net/3FxQE/

4

2 に答える 2

0

セレクターで、必要のないコンテキストを使用しようとしています。とで変更$('li *','div#contentWrapper')します。働くフィドルはここにあります$('div#contentWrapper')$(event.target)$(e.target)

$('div#contentWrapper').on('click', '#interfaceContainer #left #sortableTestComponents li', function (e) {
    e.stopPropagation();
    var $this = $(this),
        domEl = $this.get(0);
    alert(domEl.tagName);
    if (!$(e.target).hasClass("deleteTestComp")) {
        var type = $this.find('div.headerName').html(),
            information = $this.find('input[type=hidden]').val();

        if (type == "CliSessionType") {
            parseCliComponent(information);
        } else if (type == "DBSessionType") {
            parseDbComponent(information);
        } else {
            parseScreenComponent(information);
        }
    }
});
于 2013-03-26T03:11:27.580 に答える
0

に該当する要素のclickイベントに関心があるため、イベント登録は次のようにする必要がありますli#interfaceContainer$('#interfaceContainer').on('click','li',function(e){...});

次に、を取得するにはtagName、これが利用可能なイベントの実際のソースを使用する必要があるため、クリックされたdom要素を取得するためe.targetに使用する必要があります。$(e.target).get(0)

$('#interfaceContainer').on('click','li',function(e){
    e.stopPropagation();
    var domEl = $(e.target).get(0);
    alert(domEl.tagName);
    if(!$(event.target).hasClass("deleteTestComp")){
        var type = $(this).find('div.headerName').html();
        var information = $(this).find('input[type=hidden]').val();

        if(type == "CliSessionType"){
             parseCliComponent(information);
        }
        else if(type == "DBSessionType"){
            parseDbComponent(information);
        }
        else{
            parseScreenComponent(information);
        }
    }
});

デモ:フィドル

于 2013-03-26T03:18:57.030 に答える