检查图像是否具有标题属性,然后应用于图像选择器
Check if image has title attribute, then apply to image selector
这似乎对我不起作用。我只想在设置了title属性的图像上执行这些操作。我的问题似乎是,当我使用$(this)
时,它指的是title属性?提前谢谢。
(function($) {
$(function() {
/* Run this only if images have a title attribute */
if ($('.node-page img[title], .node-news img[title]')) {
$(this).each(function() {
var image = $(this);
var caption = image.attr('title');
var imagealign = image.css('float');
image.after('<span class="caption">' + caption + '</span>');
image.next('span.caption').andSelf().wrapAll('<div>');
image.parent('div').addClass('caption-wrapper').css({'width': imagewidth, 'height': 'auto', 'float': imagealign});
});
}
});
})(jQuery);
您似乎混淆了if
和each()
。您应该将您的each直接应用于选择器。如果没有图像具有title
属性,那么它不会执行任何操作,否则它会将您的函数应用于每个元素。
(function($) {
$(function() {
/* Run this only on images that have a title attribute */
$('.node-page img[title], .node-news img[title]').each(function() {
var image = $(this);
var caption = image.attr('title');
var imagealign = image.css('float');
image.after('<span class="caption">' + caption + '</span>');
image.next('span.caption').andSelf().wrapAll('<div>');
image.parent('div').addClass('caption-wrapper').css({'width': imagewidth, 'height': 'auto', 'float': imagealign});
});
});
})(jQuery);
相关文章:
- JS-移动图像,暂停设置的时间,然后将图像向后移动
- 先加载特定的图像,然后再加载页面的其余部分
- Pinterest 扩展如何存储(临时)网页中的图像,然后在 iframe 中访问它们
- 单击时预置图像 - 仅预置一次,然后停止
- 单击从一张图像切换到两张图像,然后再切换回一张图像
- 将普通的 PNG 图像转换为 JSON 数据,然后存储到对象
- 将图像输出到浏览器,然后再将其保存到文件夹
- 图像的尺寸,然后检查标准
- 在html画布中显示一个图像,然后增大其大小
- 在首页动画化一个标志3秒钟,然后显示一个图像滑块
- 在上传之前预览图像,然后以aspmvc上传到服务器
- 如何在后台加载图像,然后在完成后显示页面
- 将生成的随机数存储在变量中,然后将该变量作为文件名调用以显示图像.Javascript
- 如何检测移动浏览器(PHP/Javascript),然后进行图像/文本更改
- 在一个简单的jquery幻灯片中,我的类到达图像的末尾,然后从DOM的边缘掉下来
- JavaScript:从javascript加载图像,然后等待该图像的“load”事件
- 如何获取页面上所有图像的来源,然后将它们全部放在一个分区中
- 如何使用画布更改图像的不透明度,然后将其加载到画布中的另一个图像上?-HTML
- 当缩放浏览器窗口到给定的浏览器窗口宽度时,向下裁剪图像.然后让图像的其余部分响应
- jQuery在悬停时更改图像(然后返回到初始图像)