jQuery:>动态元素

发布于 2024-08-11 16:44:55 字数 612 浏览 7 评论 0原文

这是我的代码有问题的部分,在 .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 可以正确识别它。 $img.html() 返回空值,不是未定义,而是空白。测试了 FF 3.6 和 IE8。有人可以解释一下吗?

Here is the problematic part of my code, run inside .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());
});

First, i'm working with the selected tinyMCE content in html format which is fust fine as jQuery recognizes it properly. $img.html() returns empty value, not undefined but just blank. Tested both FF 3.6 and IE8. Can someone explain, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰之心 2024-08-18 16:44:56

html() 函数仅返回元素的内容。由于 从技术上讲是空的,因此您会得到一个空字符串。

如果你有这个:

<span>The text</span>

并且你要求 $span.html(),你只会得到 文本,没有封闭的标签。

The html() function only returns the contents of the element. Since <IMG> is technically empty, you get an empty string.

If you had this:

<span>The text</span>

and you asked for $span.html(), you'd get just The text, without the enclosing tags.

过去的过去 2024-08-18 16:44:56

如果您有兴趣访问 的内容,请查看 问题号 298049

If you are interested in accessing the <img>'s content then have a look at Question number 298049

迷爱 2024-08-18 16:44:56

.html() 为您提供元素的innerHTML,即什么在里面。 内部没有任何内容 - 它是一个空元素。

通过包装它并获取其innerHTML,您就有了正确的想法。

.html() gives you the innerHTML of an element i.e. what's inside it. An <img> doesn't have anything inside it - it's an empty element.

You have the right idea by wrapping it and taking the innerHTML of that.

迷你仙 2024-08-18 16:44:56

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

An image element doesn't have any inner HTML, therefore the method can't return any.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文