本帖最后由 p2227 于 2011-05-09 21:29:03 编辑

解决方案 »

  1.   

    //5
    $("#temp img").each( function() {
    if ($(this).width > 625) $(this).width = 625;
    });
      

  2.   

    发现要这样才能正确运行,不过在您的提示下5也解决了
    $("#temp img").each(function(){
    if($(this).width()>625){
    $(this).attr("width","625");
    //$(this).width(625);
    }
    });
      

  3.   

    //1
    $("#temp img").each( function() {
    if (!$(this).parent().is('a')) $(this).wrap("<a></a>");
    });
      

  4.   

    //4
    $("#temp p:has(img)").remove();
      

  5.   

    您的操作会把p里面的子元素img一起移除的……
    不过我总结了一下,结合新需求,主要代码如下:/*
    此程序想要达到的文本修改效果
    1.外围没有包裹a的img增加a元素(a中有如下属性:rel=shadowbox,href=img的src)
    2.子元素包含img的a,属性修改为(rel=shadowbox,href=被包含img的src)
    3.img的alt属性改为“点击查看大图”
    4.如果p的子元素是img,则把p换成div
    5.如果img width<=625,则不改,如果img width>625,则width=625
    */
    $("#add").click(function(){ $("#temp").html($("#src").val()); //process functions
    //1
    $("#temp img").each( function() {
    //5
    if($(this).width()>625){
    $(this).attr("width","625");
    } //4 step 1
    if($(this).parent().is("p")){
    $(this).wrap("<div></div>");
    }
    }); //4 step 2
    $("#temp div").each(function(){
    if($(this).parent().is("p")){
    $(this).parent().after($(this));
    //$(this).parent().remove();
    }
    });
    //4 step 3
    $("#temp p:empty").remove(); //1
    $("#temp img").each( function() {
    if (!$(this).parent().is("a")){
    $(this).wrap("<a></a>");
    }
    });
    //2
    $("#temp a").has("img").attr("rel","shadowbox");
    $("#temp a").has("img").each(function(){
    this.href=$(this).children("img").attr("src")
    });  //3
    $("#temp img").attr("alt","点击查看大图"); $("#opt").text($("#temp").html()); $("#temp").empty();
    }
    );
      

  6.   

    /*
    此程序想要达到的文本修改效果
    1.外围没有包裹a的img增加a元素(a中有如下属性:rel=shadowbox,href=img的src)
    2.子元素包含img的a,属性修改为(rel=shadowbox,href=被包含img的src)
    3.img的alt属性改为“点击查看大图”
    4.如果p的子元素是img,则把p换成div

    5.如果img width<=625,则不改,如果img width>625,则width=625*/