猿问

JQuery批量操作DOM元素

<div id="dsize">
    <div id="1">111</div>... 批量生成的div...<div id="N">454</div>   

</div>


var $dsize = $("#dsize");
            for (var i = 0; i < $dsize.length; i++) {
                $dsize.eq(i).click(function () {
                    $dsize.eq(i).addClass("on");//为什么加不上样式
                    //$dsize.eq(i).css("color", "red");//这样也加不上样式
                })
            }



补充:

这是完整的代码包括取值

$(document).ready(function () {
            var ID = $("#txtID").val();
            var url = $.format("../../Controls/InfoHandler.ashx?param={0}", "getinfo");
            $.getJSON(url, function (data, status) {
                for (var i = 0; i < data.length; i++) {
                    $("#dsize").append(
                        "<div id="+data[i].InvID+">" + data[i].Size + "</div>"
                        );
                }
            });
            var $dsize = $("#dsize");
            for (var i = 0; i < $dsize.length; i++) {
                $dsize.eq(i).click(function () {
                    $dsize.eq(i).addClass("on");
                    //alert($dsize.eq(i).attr("class"));
                    //$dsize.eq(i).css("color", "red");
                })
            }
        });

田青国4016870
浏览 1688回答 2
2回答

Caballarii

var $dsize = $("#dsize");改成var $dsize = $("#dsize").children();

qq_昼绽_0

$dsize.length什么意思,难道还有几个?
随时随地看视频慕课网APP

相关分类

JQuery
我要回答