【发布时间】:2010-10-26 17:15:42
【问题描述】:
尝试用一些 php 服务器端的魔法替换 this jquery code:
$(document).ready(function() {
$('#text img').each(function(){
source = $(this).attr('src');
$(this).wrap($('<div class="caption '+ $(this).attr('class') + '"></div>')).removeAttr('class').removeAttr('height').removeAttr('width');
$(this).after('<p>' + $(this).attr('alt') + '</p>');
$(this).attr('src', '/system/tools/phpthumb/phpThumb.php?src=' + source + '&wl=200&hp=200&q=85&f=jpg');
});
});
它所做的就是这样做:
<img class="right" src="blah.jpg" alt="My caption" width="100" height="200" />
并将其替换为:
<div class="caption right">
<img src="/system/tools/phpthumb/phpThumb.php?src=blah.jpg&wl=200&hp=200&q=85&f=jpg" alt="My caption" />
<p>My caption</p>
</div>
图像分散在<p> 之间的一块纺织格式的 html 中。像这样的:
<p>My paragraph text</p>
<img src="image.jpg" />
<p>Another paragraph text <img src="another_image.jpg" /> with more text</p>
<p>And so on</p>
它使用户能够将图像向左、向右或居中浮动,并且缩略图是使用 phpthumb 自动创建的。我假设我需要使用正则表达式。我是 php 新手,只知道前端编码。你会如何攻击它?
【问题讨论】:
-
这不是您问题的答案,但它仍然很重要。您尝试输出的结果标记无效,因为它具有嵌套在内联元素中的块元素。 标签不应该包含
标签,也不应该包含
标签。
-
替换的语法仍然无效,因为
节点被
关闭。 -
img 元素是内联的。它在像 span 或 a 这样的内联元素中完全有效。