代码之家  ›  专栏  ›  技术社区  ›  Z. Zlatev

jQuery:<IMG/>动态元素

  •  0
  • Z. Zlatev  · 技术社区  · 16 年前

    下面是我代码中有问题的部分,在.each(function(){})中运行;

    $('img','<div>'+ed.selection.getContent({format: 'html'})+'</div>').each(function(){  
      $img=$('<img/>').attr('src',$(this).attr('src'));
      alert($('<p>'+$img+'</p>').html());
      if ($(this).attr('height').length>0){
        $img.attr('height',$(this).attr('height'));
      }
      if ($(this).attr('width').length>0){
        $img.attr('width',$(this).attr('width'));
      }
      alert($img.html());
    });
    

    首先,我正在使用html格式的选定tinyMCE内容,这很好,因为jQuery能够正确识别它$html()返回空值,不是未定义的值,而是空值。测试了FF 3.6和IE8。有人能解释一下吗?

    4 回复  |  直到 16 年前
        1
  •  2
  •   Seb    16 年前

    这个 html() <IMG> 如果从技术上讲是空的,则会得到一个空字符串。

    <span>The text</span>
    

    $span.html() ,你会得到 The text ,不带封闭标签。

        2
  •  1
  •   Community Mohan Dere    9 年前

    如果您有兴趣访问 <img> 的内容,然后查看 Question number 298049

        3
  •  0
  •   Greg    16 年前

    .html() 提供元素的innerHTML,即元素内部的内容。一 <img> 里面没有任何东西-它是一个空元素。

    你有一个正确的想法,把它包装起来,并把它的内部HTML。

        4
  •  0
  •   tvanfosson    16 年前

    图像元素没有任何内部HTML,因此该方法不能返回任何内部HTML。