JQuery用原始内容替换div

时间:2013-06-17 19:06:39

标签: jquery replacewith

我有一个下拉列表,可以调用不同的操作:

$('#Select').change(function () {
            var n = $(this).val();
            switch (n) {
                case '1':
                    // Action
                    break;
                case '2':
                    //Action
                    break;
                case '3':
                    //Action
                    break
            }
        });

我希望能够实现的是每次用户在下拉框中选择其他选项时,清除插入到结果div中的任何内容。

我有一个容器,其中显示的所有内容都是由于下拉选择而调用的,我希望在加载页面时将其恢复为原始状态。我试过了:

var divClone = $('#Everything').clone();
        $('#Select').change(function () {
            $('#Everything').replaceWith(divClone);
            var n = $(this).val();
            switch (n) {
                case '1':
                    // Action
                    break;
                case '2':
                    // Action
                    break;
                case '3':
                    // Action
                    break;
                case 'Select a Call Type':

                    break
            }
        });

但没有运气。

有什么建议吗?

先谢谢。

1 个答案:

答案 0 :(得分:2)

您没有看到任何更改,因为您正在用自己的克隆替换#Everything。净效应没有变化。

相反,您可以使用html函数设置#Everything div的内容:

var everything = $('#Everything');
$('#Select').change(function () {
    var n = $(this).val();
    switch (n) {
        case '1':
            everything.html("Hello from 1");
            break;
        case '2':
            everything.html("Hello from 2");
            break;
        case '3':
            everything.html("Hello from 3");
            break
    }
});

<强> Working Fiddle