<script>
        $(function(){
                $("input").click(function(){
                        $.alertBox("This is msg");

                })
        })
        jQuery.extend({
                alertBox:function(msg){
                        var fullBox = $("<div>",{"class":"fullBox"}).appendTo("body")
                        if ($.browser.msie && ($.browser.version == "6.0")) {
                                fullBox.append("<iframe></iframe>")
                        }                
                        var alertBox = $("<div>",{"class":"alertBox"}).appendTo("body")
                        var p = $("<p>",{
                                text : msg
                        }).appendTo(alertBox)
                        var enterButton = $("<a>",{
                                "href" : "javascript:void(0)" ,
                                "title" : "确认" ,
                                text : "确认" ,
                                click : function() {
                                        $(this).blur()
                                        $(".fullBox,.alertBox").remove()
                                }
                        }).appendTo(alertBox)
                }
        })
        </script>比如创建alertBox的时候,为什么var alertBox = $("<div>",{"class":"alertBox"}).appendTo("body")而用$('<div/>').attr('class','alertBox').appendTo('body'); 不行呢。。 是因为写在jQuery.extend里的原因吗?

解决方案 »

  1.   

    这样肯定是不行的。$('<div/>').attr('class','alertBox')attr 是对已存在的dom进行jq方法的添加,而$("<div>",{"class":"alertBox"})这个是jquery内部将class绑定到div上一块appendTo进去的。
    如果非要使用attr,那也可以这样:$('<div/>').appendTo('body').attr('class','alertBox');先添加然后再操作,这样不建议,貌似多做了一次查询。所以还是上面的那个办法靠谱。
      

  2.   

    我擦,我真是傻了,我忘记写 var alertBox=XXX了,1楼说得对。
    2楼感谢你花了这么多时间给我解释,不过先写attr,再写append是可行的,这种写法没有问题