我完全不(完全)熟悉JQuery,只能在蓝色的月亮中蘸一下脚趾。然而,我需要我根本无法找到一个快速解决方案 - 尽管我相信这样一个快速解决方案必须可用:)JQuery - 查找图像,添加来自attr的标题
我有一个div,其中的图像可能会在文本之间浮动。我想扫描那个div,选择其中的所有图像(如果有的话),给他们一个课,给他们添加一个<p></p>
,并将<img>
和匹配<p>
放在他们自己的包装div中。在创建的段落块中,我想要放置所述图像的标题属性。
我已经试过谷歌和这里,但都涂有片段处理插件,而我只是想要一个即插即用片段,我可以放在我包括的js文件中。我试图从JQuery网站上的东西中拼凑出自己的东西,但这是一团糟。
任何帮助都非常感谢!
编辑:使用由以下@ Xeon06提供了答案,我的完成工作溶液,如下所示:
$(".lorem img").each(function() {
$(this).addClass("inline-img");
$(this).wrap("<div>");
if ($(this).prop("title")!='') {
$("<p>").text($(this).prop("title")).insertAfter($(this));
}
});
$(".lorem div").each(function() {
$(this).addClass("inline-img-wrap");
});
$(".inline-img").each(function() {
var mTop = $(this).css('marginTop');
var mBottom = $(this).css('marginBottom');
var mLeft = $(this).css('marginLeft');
var mRight = $(this).css('marginRight');
var thisfloat = $(this).css('float');
$(this).parent().css('marginTop', mTop);
$(this).css('marginTop', '');
$(this).parent().css('marginBottom', mBottom);
$(this).css('marginBottom', '');
$(this).parent().css('marginLeft', mLeft);
$(this).css('marginLeft', '');
$(this).parent().css('marginRight', mRight);
$(this).css('marginRight', '');
$(this).parent().css('float', thisfloat);
$(this).css('float', '');
});
代码的最后部分是采取的图像的主定位属性(裕度和浮动)并将它们应用于之前创建的包装div。然后,我从图像中自己抹去这些值以避免加倍。打扰一下noobish编码吧!
你实际上不能追加anythi ng''标签,但您可以在''之后附加它。 – Blazemonger 2012-01-05 20:25:23
如果你的意思是它会在图片标签后出现,那就没问题了。假设两者都将包裹在他们自己的div中。 – Eamonn 2012-01-05 20:28:35