現在、私はこのJQueryスクリプトを持っています
var img = $(this);
img.wrap('<div class="photo"></div>');
img.parent().append("<p>" + img.attr("alt") + "</p>");
これにより、次のことが正常に行われます。
<img src="photo.jpg" alt="caption">
これに
<div class="photo">
<img src="photos.jpg" alt="caption"/>
<p>caption</p>
</div>
画像に親としてのリンクがない限り、すべて問題ありません。<a href="#"><img
私はそれを正しいhtmlにしたいので(私はうるさいです)、以下の結果の結果を改善することは満足のいくものです
<a href="#">
<div class="photo">
<img src="photos.jpg" alt="caption"/>
<p>caption</p>
</div>
</a>
これに:
<div class="photo">
<a href="#">
<img src="photos.jpg" alt="caption"/>
</a>
<p><a href="#">caption</a></p>
</div>
これはこれまでの私のjQueryスクリプトです(これは動作しませんbc私は初心者です)ああ
if(img.parent('a')){
var targetLink = img.parent('a').attr('href').val();
img.parent('a').wrap('<div class="photo"></div>');
img.parent('div').append('<p><a href="'+targetLink+'">' + img.attr("alt") + '</p></a>');
}else{
img.wrap('<div class="photo"></div>');
img.parent().append("<p>" + img.attr("alt") + "</p>");
};
アドバイスや助けをいただければ幸い
です:)ありがとうございます!
回答を更新
var withLink = img.parent('a').length > 0,
targetPar = withLink ? img.parent() : img;
targetPar.wrap('<div class="photo"></div>');
if(withLink > 0){
targetPar.parent().append('<p class="caption"><a href="'+img.parent().attr('href')+'">' + img.attr('title') + '</p></a>');
}else{
img.parent().append('<p class="caption">' + img.attr('title') + '</p>');
}