[英]Getting the title of an image and display it as HTML using JQuery
I have a img which is displaying fine, however I now want to hide the image (display: none) and get the value of its title and display that in its parent div instead, using JQuery.我有一个显示正常的 img,但是我现在想隐藏图像(显示:无)并获取其标题的值并使用 JQuery 将其显示在其父 div 中。 I'm sure it's relatively simple, I'm just drawing a massive mind blank at the moment.
我敢肯定它相对简单,我现在只是在画一个巨大的大脑空白。
<div class="cardBrand image">
<img class="card-logo" title="Mastercard" src="/hosted/assets/payment-logo/mastercard-debit.png">
</div>
You can insert the title after the image like您可以在图像后插入标题,如
$('.cardBrand.image img').after(function() { return this.title; }).hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="cardBrand image"> <img class="card-logo" title="Mastercard" src="/hosted/assets/payment-logo/mastercard-debit.png"> </div>
You can get the title of the image using $('#img-id').attr('title');
您可以使用
$('#img-id').attr('title');
获取图像$('#img-id').attr('title');
and then use it to set the html using $('#div-id').html($('#img-id').attr('title'));
然后使用它来设置 html 使用
$('#div-id').html($('#img-id').attr('title'));
Note : You can use the class also.
注意:您也可以使用该类。 But, it will change all the div with the same class.
但是,它会更改所有具有相同类的 div。 It is recommended that you use an id for the div for these purposes
出于这些目的,建议您为 div 使用 id
var image_title=$('.card-logo').attr('title'); // get title of image
$('.card-logo').hide(); // hide image
$('.cardBrand').text(image_title); // set title in parent div
var imgTitle="<span>" + $(".card-logo").attr("title") + "</span>";
$(imgTitle).insertAfter($(".card-logo"));
$(".card-logo").hide();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.