我正在尝试检索title属性并用它替换当前段落文本.关于我的脚本的其他所有内容都完美无缺.title属性将返回"undefined".我是javascript的新手,我已经看了很多答案......到目前为止我没有看到任何东西.
JS:
$(document).ready(function(){
$('.thumbnails a').click(function(e){
e.preventDefault();
$('.thumbnails a').removeClass('selected');
$('.thumbnails a').children().css('opacity','1');
$(this).addClass('selected');
$(this).children().css('opacity','.4');
var photo_caption = $(this).attr('title');
var photo_fullsize = $(this).attr('href');
var photo_preview = photo_fullsize.replace('_large','_preview');
$('.preview').html('<a href="'+photo_fullsize+'" title="'+photo_caption+'" style=background-image:url('+photo_preview+')></a>');
$('.caption').html('<p><a href="'+photo_fullsize+'" title="'+photo_caption+'">View larger</a></p><p>'+photo_caption+'</p>');
});
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<li>
<a href="images/webdesign/indiePantry_large.jpg" target="_blank">
<img src="images/webdesign/indiePantry_thumbnail.jpg"
width="160" height="160" title="Indie Pantry" />
</a>
</li>
Run Code Online (Sandbox Code Playgroud)
ade*_*neo 12
标题位于图像上,而不是<a>元素:
var photo_caption = $('img', this).prop('title');
Run Code Online (Sandbox Code Playgroud)
BLS*_*lly 12
在您的代码中,this引用a标记,而不是图像.
尝试:
$('img', this).attr('title');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
167 次 |
| 最近记录: |