仅在单击对象后放置div

时间:2011-11-25 14:54:52

标签: javascript ajax jquery

Buildgames返回行:

<a>....</a>
<a>....</a>

当我点击每个a时,Buildcar_s函数会返回警报中的所有数据。

我希望将所有结果放在每个div下的a,而不是此警报,所以它看起来像:

<a>.....clicked ...</a>
<div>....
    ...
</div>
<a>....not clicked...</a>
<a>....not clicked...</a>
<a>....not clicked...</a>

我们怎样才能在点击的div之下添加a

function Buildcar_s(items) {
    var div = $('<div/>');
    $.each(items, function() {
        var car_ = this.car_;
        $('<a>' + this.car_ + '----' + this.Names + '---' + '</a>').click(function() {
            _Services.invoke({
                method: 'GetgamesRows',
                data: {
                    car_Number: car_
                },
                success: function(car_s) {

var div = Buildgames(car_s);
                         $(div).insertAfter($a);
                }
            });
        }).appendTo(div);
        $('<br/>').appendTo(div);
    });

    $("#leftRows").append(div);
}

function Buildgames(items) {
    var place = '<div>';
    $.each(items, function() {
        place += 'mmmmmm<br/>';
    });
    place += '</div>';
返回地点;     }

1 个答案:

答案 0 :(得分:2)

试试这个,相关的变化已被评论:

function Buildcar_s(items) {
    var div = $('<div/>');
    $.each(items, function() {
        var car_ = this.car_;
        $('<a>' + this.car_ + '----' + this.Names + '---' + '</a>').click(function() {
            var $a = this;
            _Services.invoke({
                method: 'GetgamesRows',
                data: {
                    car_Number: car_
                },
                success: function(car_s) {
                    var div = Buildgames(car_s);
                    // this inserts the HTML generated from the function, 
                    // under the A element which was clicked on.
                    $(div).insertAfter($a); 
                }
            });
        }).appendTo(div);
        $('<br/>').appendTo(div);
    });

    $("#leftRows").append(div);
}

function Buildgames(items) {
    var place = '<div>';
    $.each(items, function() {
        place += '<div style="float: right;">&nbsp;&nbsp;&nbsp;' + this.CITY + '&nbsp;&nbsp;&nbsp;' + '</div><BR />' + +'<br/><br/>';
    });
    place += '</div>';

    return place; // returns the string created, as opposed to alerting it.
}