我遇到了一个小问题,我正在尝试通过从 alt 标签获取图像标题来打印图像标题。这不起作用,因为它只显示 alt 中的第一个单词。
$("#galleryThumbs .imageThumb").click(function () {
if ($(this).hasClass('active')) {
//do nothing
} else {
var newImage = $(this).attr('picURL');
$('#galleryImageLarge').attr('src', newImage);
$("#galleryThumbs .imageThumb").removeClass('active');
$(this).addClass('active');
var newTitle = $(this).attr('alt');
$('#galleryTitle').text(newTitle);
}
});
HTML 代码:
<div class="col-md-6 verticalcenter work" id="largeGalleryImage">
<img src="<?php echo $galleryImages[0]['sizes']['large']; ?>"
alt="<?php echo $galleryImages[0]['title']; ?>"
class="img-responsive" id="galleryImageLarge" />
<p id="galleryTitle"><?php echo $galleryImages[0]['title']; ?></p>
</div>
最佳答案
您应该转义从 PHP 传递到 HTML 的变量,即尝试替换:
<?php echo $galleryImages[0]['title']; ?>
与:
<?php echo htmlspecialchars($galleryImages[0]['title']); ?>
您的 $galleryImages[0]['title']
可能有 "
符号,该符号将被解释为属性值的结尾。
关于javascript - .attr 'alt' 仅显示第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31289761/